How to scan one part of a file as a variable - c

I'm a beginner and I have a code that requires me to scan the last number of a file into the variable a.
The file looks something like this:
car,4,50,800
If I want to scan the "800" into the variable a, how do I do that?

In order to do it you have to read the data first and find the last occurance of the ',' char. Then you can get the data till let's say the end of file (EOF) or the new line char (\n). 8t all depends by the protocol you are using to store your data.

Related

Reading the text file every time or Get content and store it with dynamic array in C Programming, which is best idea?

I have the text file with 2000 lines and each line contains up to 256 characters.
I need to detect EOF as I'm reading the file.
Should the code process a line as it is read or should it read the lines into an array and then process the array one element at a time?
If the data don't change in your file, you should stock the data in an array.
In the other case, you have to read your file each time you need to check a line.

How can I search for a specific string in a csv file, and to know in which line that string was found?

I started to learn C and I am writing a code where the user needs to type a string he wants to search in the csv file , and if the string was found , it will print the number of line the string was found at .
and I am not exactly sure what is the best way to do that
You may read line by line using getline() until it returns -1 (EOF), keeping a counter on how many iterations you have done.
For each line you read, you can use strstr() to check if the string is present. If yes, just print the counter (that will correspond to the line).

C - dynamically modifying a file - is it possible?

I'm writing a small program in C and I want to have the option of saving data to file and then reading it from that file. The data is BIG, so I want to somehow dynamically write to a file without having to create a new file and copy modified old file into it.
Here's exactly what I want to do:
In the first line, I want to have "description" of the data in the form "%s %s %s ... %s \n" where %s is a string and the n'th string describes data in n+1'th line. I want to read the 1'st line of the file, scan for corresponding "description" string, and if it is not present, append it to the first line, and the data corresponding to it after the last line of the file.
The question is - is it possible to "jump" into lines in the file without scanning all the previous lines, and can I somehow read the first line of the file and append something to it after reading? Or maybe it is not the way to go in this situation and C offers some kind of different solution?
What you want can be done using stdio and fseek(). As long as you know at what byte offset you want to go, you can overwrite and/or append anywhere in the file without reading the data before, or the data you're overwriting. What you can not easily do is insert data, i.e., open the file, split it in half and put data in between.
Not too sure if that is what you mean though...

Comparing a string from text file and then printing out the entire line(s)

My goal is to print out every full line from a text file if that line contains a string that is equivalent to user input.
I understand how to find the occurrences of a specific string in a text file, but I am confused as to how to associate that with a specific line. How do I relate my string with the specific line that it is in?
My initial thought was to store each line in an array and then print out that line if the user string is somewhere in that line.
However each line is a different size, so I was wondering if it is possible for me to initially divide my entire text file into x number of lines and then use a loop to go through each line and search for that string?
Save the file pointer of the starting of the line in a temp variable before starting new line compare

C - sorting a list on words in a text file

Wondering what method would be most suitable, and maybe, if someone would be so kind, how to start such a function for:
AWord
DWord
CWord
BWord
To end up in a new text file like:
AWord
BWord
CWord
DWord
At the moment, my program, reads each word line by line fgets() and does some stuff to it, capitalises the first letter etc. Once all that is done, a new text file is created, text.out.
I then want to sort it into alphabetical order, since all the words are only alphabetic, some maybe terminating with a number.
Thanks, any help is appreciated!
T.C
You said that you don't have an array of words, but that you read each line into a separate character array. This is what you need to fix. Declare char *lines[MAXLINES], an array of every line from your input file. Read each line from your input file in to that array. Then you can sort that array before putting it back out.
This is pretty much identical to the concepts presented in K&R Section 5.6 and 5.11.

Resources