Huy
Huy

Reputation: 11206

Trying to add null character to end of a char array before strcpy

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;
}

Upvotes: 1

Views: 15619

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726939

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

Upvotes: 12

Related Questions