C - sorting a list on words in a text file - c

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.

Related

How can I read from a text file, reverse every word but not their places, and then write it into another text file?

lets say we have an input text file and an empty output text file. The program supposed to reverse every word in the input file but not their places then write it in the output file.
How can I obtain the following output?
input:
bacon is tasty and crispy.
Orange is overrated.
output:
nocab si ytsat dna ypsirc.
Egrango si detarrevo.
Well I'm not going to just provide an answer, this looks like homework. The way I would approach it is:
Interesting characters are : '\n' and ' '.
Ready char by char
Check if you have encountered any interesting characters
If you have not, store the character read into an array
If you have, print the array (make sure its not empty), starting from the last character saved.
Go to 1 (until EOF)

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

removing lines and characters while reading into an array

Trying to write a shell script that will read in a text file which looks something like:
Line A needs to be removed
Line B also to be removed
Line C which has lots of things, including characters that need removing should be the first to be read into an array position [0]
Line D
.
.
Line "n"
What I need to do is read from line C up to line n-1 into an array, but also remove the first 4 characters and the last 2 characters of the useful lines (Line C to Line n-1).
I can't seem to do anything other than read in the entire list, or print/echo the partial list but can't get that into an array.
I'm happy to multi-step it, rather than do it all in one line, but what ever is clean.
Try this if works
head -n-1 txt | awk '{if($0~/LineC/){i=1}; if(i>0){print substr($0,5,length($0)-2)}}' txt

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

Reading from a file

hello i got a problem with reading from a file, i am trying to read from a file using fscanf() and i cant seem to sort it out.
i try to read the file line by line and putting the string in a variable (buffer) each time but i cant understand how the while loop is suppose to be looking like
thanks in advance
the file that i want to read from is a txt file with this format: first line :"1234,abc,etc" second line : "2432,fjh,etc" and more lines like those i want to be able to use the fscanf method inorder to put in each loop the all line lets say "1234,abc,etc" in my string variable and so on till i dont have any more lines to read from
this is what i managed to gather so far (ofc its not the currect way to write it):
char* buffer[100];
while (fscanf(FILE *finput,"%s",buffer)!=something)
{
printf("%s",buffer);
}
i want this code to be able to print all of the lines in my code if you would be able to correct my errors i will greatly appriciate it
I feel like you should read some of these great topics first:
Trouble reading a line using fscanf()
Reading file using fscanf() in C
fscanf multiple lines [c++]
There are plenty of reasons why you should use fgets or something else instead.
Quoting from this place:
fscanf() is a field oriented function and is inappropriate for use in a robust, general-purpose text file reader. It has two major drawbacks:
You must know the exact data layout of the input file in advance and rewrite the function call for every different layout.
It's difficult to read text strings that contain spaces because fscanf() sees space characters as field delimiters.
If you know the size of file you're trying to read, you could use fread(), which is block oriented.

Resources