-1

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

3
Contributors
15
Replies
17
Views
6 Years
Discussion Span
Last Post by bojomojo
0

Declare a structure as follows:

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

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

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 by Shankye: n/a

Comments
Agreed
0

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

0

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 by myk45: n/a

0

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

0

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 by Nick Evan: Added CODE tags

0

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 by myk45: n/a

0

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

0

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 by Nick Evan: Added ICODE tags to preserve formatting

0

Can you clearly state your problem ?

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

0

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 by Nick Evan: Added CODE-tags

0

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

0

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

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.