1

I have a function that reads in a file that looks like the following into a structure. I'm trying to make sure that the structure is being filled properly; and, it is filled correctly for Gender, Height, and Weight. However, I'm having trouble verifying that the Name (character array) part of it is filling properly.

Example file to read in:

Name,Gender,Height,Weight Tanner,M,71.8,180.25 John,M,70.75,185.3 Parker,F,65.25,120.3 Meeks,M,57.25,210.2 Big,M,57.5,150.1 Jackson,F,52.1,163.4 

Struct Definition:

struct canData { char name[50]; char gender; float height; float weight; }CD[structSize]; // end struct BP 

Part of the loop that reads the file in:

char name[50]; char gender; float height; float weight; int position = 0; filePtr = fopen(fileName, "r"); // open file if (filePtr == NULL) // error check opening file { printf("Opening file failed. Please reenter filename."); exit(1); // WILL THIS RETURN TO MENU? } // end if // skip header line of file char buffer[100]; fgets(buffer, 100, filePtr); while (fscanf(filePtr, "%[^,], %[^,], %f, %f", &name, &gender, &height, &weight) == 4) // read in { printf("%s\n", name); // DEBUG ATTEMPT printf("%s\n", CD[position].name); // DEBUG ATTEMPT printf("%f\n", weight); // DEBUG ATTEMPT strcpy(CD[position].name, name); CD[position].gender = gender; CD[position].height = height; CD[position].weight = weight; position++; iCount++; } // end while 

Currently, my output is as follows:

(space where name should be) (space where CD[position].name should be) 180.25 (space where name should be) (space where CD[position].name should be) 185.3 (space where name should be) (space where CD[position].name should be) 120.3 (space where name should be) (space where CD[position].name should be) ... 

Thanks for any insight! I'm a C beginner, so I may be missing something silly.

10
  • 1
    What are types of name, gender, height and weight? Are you sure you are not invoking undefined behavior by passing pointers to object having wrong type? Commented May 1, 2016 at 5:13
  • 1
    Are you sure CD[position].name in the DEBUG ATTEMPT initialized? Please post a Minimal, Complete, and Verifiable example. Commented May 1, 2016 at 5:13
  • 3
    You print CD[position].name before the strcpy. Also, try while (fscanf(filePtr, " %[^,], %[^,], %f, %f", name, gender, &height, &weight) == 4) Commented May 1, 2016 at 5:20
  • 2
    gender is capable to store only one character, so only zero characters can be read, considering the terminating null-character. Commented May 1, 2016 at 5:28
  • 3
    @MomoDevi It seems that it happened to seem working. Do not access where is not allocated. Use %c to read single character. Commented May 1, 2016 at 5:33

1 Answer 1

1

scanf expects a pointer to initial byte of an array, when you are reading a string. So dont use& when you are passing as an argument. fscanf(fp, "%[^,]", name) should work.

name will be converted to a pointer when used in an expression.

http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html

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

1 Comment

"name itself is a pointer in your code." No, name is not a pointer but an array. Arrays used in expressions are ordinally converted to a pointer to its first element. Commonly-used exceptions are operand of sizeof operator and unary & operator.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.