Hello,

If I have a text file with the format:


Name = MattPhilip
Age = 22

how can I use C to read from the file and fill in a structure?

Thanks

Declare a structure as follows:

struct read_file {
        char name[100];
        int age;
} var;

fscanf(fp, "%d = %s", &(var.age), var.name);

But we have both char = char and char = int lines ...

Is it not better if he uses both char and than convert to int wherever necessary ..

struct read_file {
        char first[100];
        char second[100];
} var;

fscanf(fp, "%s = %s", var.first, var.second);

Edited 5 Years Ago by Shankye: n/a

Comments
Agreed

I only want to read the part after the "="
will that work in that case?

Also can you please provide a brief discription as I want to read a file with a lot of values

Name
Age
School

and alot of other values..

Well, in that case, try this:

fscanf(fp, "%*s = %s", variable_name);

eg:

dddddd = data

Now, data is read into the variable specified. So, we are basically skipping the part before the "=" and using the data after that.

Edited 5 Years Ago by myk45: n/a

Use in a loop and scan everything u want ..

If you dont scan LHS how u 'll get to know what u scanned name or age?

If you dont want LHS, just discard it after scanning..

Okay, its going fine till now..
the problem is the file I am reading from might have comments like:

Name = Matt
# this is a comment
Age = 23

how can I ignore the comments? and lines that are empty as well


Edit: I faced another problem:

I check the first string, if its "Name" I want to save it to var.Name,
however its failing, all comparisions are giving false

if(Type == "Name")
	      {
	    	  strcpy(var.Name,value);
	      }

Edited 5 Years Ago by Nick Evan: Added CODE tags

Well, here is a thought.

if (fscanf(fp, "# %s", variable)) {
               /* It is a comment. Perform required operation */
} else {
       fscanf(fp, "%*s %s", variable);
       // Not a comment.
}

Edited 5 Years Ago by myk45: n/a

Use string comparison function ..

int strcmp (const char *s1, const char *s2)

and btw

if (fscanf(fp, "# %s", variable)) {
               /* It is a comment. Perform required operation */
} else {
      fscanf(fp, "%s = %s", var.first, var.second);
       // Not a comment.
}

this may help you neglect commented line(#) but you may add another line to neglect blank line also ..

okay, everything is good till now..

what if I want the file I read to be like:

####################################################################
#                                                                  #
#                                                                  #
#                                                                  #
#                                                                  #
#                                                                  #
#                                                                  #
####################################################################

then the contents, these gives out segmentation fault.. any clues?

Edited 5 Years Ago by Nick Evan: Added ICODE tags to preserve formatting

Can you clearly state your problem ?

Post your code, error msg you getting .. And what actually you are trying to do?

I solved the error.. but now I have a problem
I am using:

char a[30];
char b[30];
fscanf(FileName,"%s = %s \n",a,b);

when I am reading the line: Age = 22 and try to save it to the struct var by:
var.Age = b;

the value stored is not 22, instead its 228.... a long number

Edited 5 Years Ago by Nick Evan: Added CODE-tags

I am, and I managed to solve the previous error using atoi..

The thing is now I want to ignore any sentence if it starts by on "#" or more

It didnt help ??

if (fscanf(fp, "# %s", variable)) {
               /* It is a comment. Perform required operation */
} else {
      fscanf(fp, "%s = %s", var.first, var.second);
       // Not a comment.
}

No it didn't, may be because comments consist of more than one string
and its giving me another problem..
After reading the last line of file, the feof doesn't exit the loop, and it gives a segmentation fault on:

fscanf(fp, "%s = %s", var.first, var.second);
This article has been dead for over six months. Start a new discussion instead.