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

Recommended Answers

All 15 Replies

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);
commented: Agreed +1

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.

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

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

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?

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

oh ...

You not you directly scan data to struct as said before ??

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.