1

Working on a problem where I have to read data from a file into a struct using .

The file is organized so that there is a name, a few lines of ASCII art terminated by a # and a rating. Here is an example

Sample Name
( S )
( S )
# 5

I have my struct set up like this:

typedef struct
 {
   char* name;
   char* art;
   int rating;
 }CASE;

When I compile my source, I keep getting the following warnings:

multiple-character character constant
overflow in implicit constant conversion

on this line buffer[artCount] = '/0'; where artCount is strlen of the buffer itself.

I'm simply adding a null character to the end of a character array to prepare for a strcpy. Is there something wrong with my logic here?

function:

/*CASE* all is an empty array of CASE structs*/
void readFile(FILE* FPin, CASE* all)
{
  CASE* walker = all;
  int count = 0;
  int artCount;
  char buffer[160];

  if((FPin = fopen("art.txt", "r")) == NULL)
  {
    printf("Error opening file.");
    exit(100);  
  }

 walker->name = (char*)malloc(sizeof(char)*100);

 /*Reads in the name*/
 while(fgets(walker->name, 100, FPin) != NULL)
  {

 /*Reads in the art*/
   while(fscanf(FPin, "%c", buffer) != '#');

   artCount = strlen(buffer);
   buffer[artCount] = '/0';
   walker->art = (char*)malloc(sizeof(char)*160);
   strcpy(walker->art, buffer);

/*Reads in the rating*/

     fscanf(FPin, "%d", &walker->rating);

   count++;
   walker++;
 } 

  fclose(FPin);
  return;
}
5
  • 6
    The escape sequence is \0backslash zero Commented Apr 23, 2012 at 21:45
  • 1
    sizeof(char) is guaranteed to be 1 by the C standard. Commented Apr 23, 2012 at 21:45
  • 6 years and nobody has pointed out that artCount = strlen(buffer); buffer[artCount] = '\0' (even with the right `\`) does nothing? Commented Jan 22, 2018 at 13:23
  • @trentcl This is so old and I haven't touched C since this class project :P Thanks for pointing it out anyway Commented Jan 22, 2018 at 23:28
  • @Huy Not really for you, more for future readers of the question. Mostly I was just surprised :) Commented Jan 23, 2018 at 0:40

1 Answer 1

12

The constant should be '\0' (with a backslash), not '/0' (with a forward slash).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.