Program which reads information from txt in structure - c

hope you can help me with this, it's kinda tricky...
i have txt file which contents names and marks of peple
for an example
Carl,Johnson,123xxx123,9;
like in order: name,surname,certificate no,mark;
struct xxxxx
{
char name[20];
char surname[20];
char certificate No[10];
int mark;
};
the problem starts when i need to read ten records from txt...
don't have clue how to duplicate struct, do i have to define it like ten times
struct xxxx sk1[ ],sk2[ ]... and so on...?

Once you have defined a struct you can declare several variables of that type.
struct xxxx sk1;
struct xxxx sk2;
or even an array of them
struct xxxx sk[10];
To populate this structure with data from the text file you need to write some code, formatted as the input is I would think fscanf() would not be of much help as is.
The approach I would take is probably reading the entire the line using fscanf() then do some splitting on ',' to get the fields. The first three are plain strcpy, the last one would take use of atoi
My grasp of C input functions are not all that good as I much prefer C++ so you should perhaps look for a second opinion in this matter.
Edit: I found some perfectly good info on how to proceed from your old questions

Related

Get datas from TXT to a matrix

I have a homework to make a file like this, License plate, date, time, speed
like: AAA-111, 2019.01.01, 12:12, 50
I have to read these into the program and check if how many cars went there at the same date, and how much % went over a speed which given by the user, then to make a file "speeding" which contain the punishment, like 50-60 100 dollar, and to check if how many cars got punished, and to write it to a file by license plate, date, and the amount to pay.
I'm new to C, and I dont know what can I do when there is strings and integers in a file.
(Actually I'm a vehicle engineer, but it's just a stuffing course which we have to do, the teached teached nothing, we have to give in this homework to get a rating)
I have tried a method which stores the file char by char ( I think) but because then I have to see it by rows and columns, a matrix would be better. But if it is possible without it, that is good too.
first you should use strucs to store each fields of a line from your file as char * attribues.
then when you have your structs, you can go on with a function that takes a line and returns a pointer to your struct:
struct ticket_t *get_ticket(char *line);
Then you should figure how to read a file line by line with getline(3) you should have a bit of code here for this.
I suggest you find the number of line the file has and then create an array of struct ticket_t* of the correct length to ease the code (memory management).
As for the other parts of you assigment you should be ok with what I told you here.
Some documentation: fopen(3), getline(3), fwrite(3), malloc(3), strcmp(3)

Writing data to file in C [Serialization]

I have a struct that I want to save in a data file.
typedef struct Photo {
char name[20];
char description[100];
} photo;
I'd like to be able to save many of these structs in a file, much like a database. The only way I can see in doing that would be through fwrite() which has problems when it comes to platform mobility, and to just write photo->name, photo->description as ascii. I would really like to avoid having to parse all of the raw text data. Is there another way to do it?
I couldn't really understand what you are asking but you can also write to files using fprintf.
And if you are asking how to store the data in the file for faster lookup, use hash tables, at the start of the file have a table which points to memory locations for the structure along with a hash value(of the name parameter).
When querying the file all you have to do is match the hash value and then jump to that specific location.

quiz questions from txt file into array in c

I am trying to get the line below into an array in c. this is for a quiz program that asks 10 questions (out of possible 15) in a random order and tracks the score and tells the user at the end how they did.
here is an example of the format for one line in my text file:
what is not an official language in Canada;English;French;Spanish;3
in other words format is string;string;string;string;int
or
question;choice-a;choice-b;choice-c;correct answer
I know how to read a file line by line in C but I do not know how to utilize these ; to divide up one line into multiple lines. Also, not sure how to keep score for this quiz after user selects their choice as an int and presses enter.
Apparently, I am supposed to define a struct to present these questions...
Any help would be greatly appreciated. Yes, this is for a final project but lectures now are over and our teacher will not provide anymore help to students outside of class hours.
Here is what I have done so far:
http://i.stack.imgur.com/E8LtH.png
Since this is for a class, I don't know if your instructor wants you to use low-tech methods or not, but I would use something like sscanf for a problem like this. You can do something like this:
int number;
char fruit[16];
char line[]="I ate 3 bananas.";
sscanf(line, "I ate %d %s.", &number, fruit);
And it updates the variables that you point to within the sscanf function accordingly.
As this is for a class project, I will provide only general guidance. I would suggest you review the strtok function, using the line from your text file as the string and a ";" as the token. Examples and documentation are available here. Repeated calls to this function will allow you to divide your line up into segments.
As for the struct requirement, a sensible way would be to create a struct with these members:
A string (char*) containing the question.
An array of strings containing the choices.
An int defining the number of choices (i.e. the length of the array in 2). You might be able to hard-code this to 3 in your example.
An int defining the correct answer.
You would create an instance of the struct for each line in your text file.
What you need is probably a tokenizer.
You may want to have a look at the discussion here Using strtok in c. A few snippet of code are listed which in my opinion, is very helpful in your case.

Print a structure in a String format in C

I one of my assignment, I have a task to print the below whole structure in a string format.
Struct test
{
int a,
char char1,char2;
}
output should be: Structure is a=10,char1=b,char2=c;
I know it is very simple by using
printf("Structure is a=%d,char1=%c, char2= %c", s.a,s.char1,s.char2);
But in real-time, I have a lot of big structures and I cannot write printf statements with access specifiers for each element of structure. Is there any other way to print the whole structure with just specifying the structure variable or some other?
There's no way to do this in pure C. Some languages support this via a concept called reflection, but it's not available in C.
Code-that-writes-code is your best bet. Write a script that finds all your structs and builds functions to printf them.
One possible solution I can think of is that you can take the help of the fread funtion using which you can save the whole content of the structure at once into a, say temporary file. Using:
fread(&STRUCTURE_OBJECT, sizeof(YOUR_STRUCTURE), 1, FILE_POINTER);
Where STRUCTURE_OBJECT is the name of a data element of your strucure.
And then use linux based commands like "cat" and "piping" etc for the quick glance of the output.

How to write dynamically allocated structure to file

I have a complex structure in a C program which has many members that are allocated memory, dynamically. How do I write this structure to a text / binary file? How will I be able to recreate the entire structure from the data read from the file.
struct parseinfo{
int varcount;
int termcount;
char **variables;
char **terminals;
char ***actions;
};
The members variables, terminals and actions are all dynamically allocated and I need to write this structure to a file so that I could reconstruct the structure later.
You must write to this structure Serialize and Deserialize functions, you can't write this structure to file as raw data because you have allocated pointers on a heap and this not make sense to save this values on file.
Short: Not in an automated way.
In essence, it depends highly on the semantics of your structure.
If the data fields inside specify the length of certain arraiys inside it,
you can reconstruct the struct.
But you have to be careful to "beleive" the values. (possible cause of stack overflow (nice word)) if you believe that there are 2^34 entries in an array.
But otherwise it is just going tru every member (pain)
You could search a little about ASN.1 and TLV-structs.
Here are some suggestions for binary serialization:
You can serialize a string either a la C (write the terminating '\0') or
by writing the size (say, an int) followed by the contents.
You can serialize an array of strings by writing the length of the array
followed by the strings.
If it's possible that you deserialize the file on a different
architecture (different int size, different endianness...), then take
care to carefully specify the binary format of the file. In such a case
you may want to take a look at the XDR serialiaztion standard.
For ASCII serialization, I like the JSON format.
The way I would suggest to do it is to think about what does it take to create the items in the first place?
When it comes to the char **variables; char **terminals; char **actions you're obviously going to have to figure out how to declare those and read them in but I don't think you can inject a /0 into a file (EOF character??)
How would you like to see it written to the file? Can you provide a sample output how you think it should be stored? Perhaps one item per line in a file? Does it need to be a binary file?

Resources