Replacing lines in a text file with C - c

I have a configuration file that I need to alter.
The structure of the file is:
resolution 12x34
interval 1234
So two strings with a whitespace delimiter. The code I use to alter it is this:
FILE *fp = fopen(configuration_file, "a+");
char str[100], key[100], value[100];
if(fp) {
while(fgets(str, 100, fp) != NULL) {
if(2 == sscanf(str, "%s %s", &key, &value)) {
if(strcmp(key, "resolution") == 0){
if(msg->resolution){
fprintf(fp, "%s %s\r\n", key, msg->resolution);
}
} else if(strcmp(key, "interval") == 0) {
if(msg->interval) {
fprintf(fp, "%s %d\r\n", key, msg->interval);
}
} else {
fputs(str, fp);
}
} else {
fputs(str, fp);
}
}
} else {
(void)printf("-- Configuration file not found (%s)\r\n --", configuration_file);
}
fclose(fp);
The idea was to read it line by line. According to the documentation for fgets says that it stops at newlines. String-scan each line and parse them into a key and value. So far so good, acting as expected. And then print the new line to the file, overwriting the line it had just scanned. This is where the problem comes in. If I use fprintf, only the first value, resolution, is processed. The result of it is:
resolution oldxres
resolution newxres
It overwrites the wrong line and skips the second entirely.
If I remove the fprintf and instead simply print the values it has found, it prints both as it is supposed to.
What am I missing here? Does fprintf push the file pointer?

And then print the new line to the file, overwriting the line it had just scanned.
Files don't work this way. Write to a new file. When finished, rename the new file to the old name. Alternatively, read the entire file into memory, change the contents in memory, then write it back.
You can rewrite individual lines is if the modified line never becomes longer than the original one. Otherwise the modified line will spill over the next line you have not read yet, and destroy it. In order to prevent this you would need some kind of look-ahead buffer, which is just too cumbersome and error-prone. In the worst case you'd need to read the entire file anyway.

Related

how to extaract data from file starting from 2nd line in c

Im very new to this language, can you help me:
Instead of making the user input col, row, and direction(scanf). I want to extract the data from file(format below)
From the file format i do not want to extract the first line(5,6), i only want to extract the remaining lines.
Below is a code of how to extract data from a file(using command line arguments), but this code extract the first line also, and only prints the lines.I do not want to print the line but to extract the data from a file instead of making the user input it.
File format:
colrow direction(starting from 2nd line)
5,6
A0 H
D0 V
C1 V
A4 H
F0 v
code of scanf
yourcolumn = getchar();
col = charToNum(yourcolumn); //function to input column
printf("enter row");
scanf("%d",&row);
printf("h: horizontally or v: vertically?\n");
scanf(" %c",&direction);
Code for extracting data from file:
#include <stdio.h>
int main(int argc, char* argv[])
{
char const* const fileName = argv[1]; /* should check that argc > 1 */
FILE* file = fopen(fileName, "r"); /* should check the result */
char line[256];
while (fgets(line, sizeof(line), file)) {
/* note that fgets don't strip the terminating \n, checking its
presence would allow to handle lines longer that sizeof(line) */
printf("%s", line);
}
/* may check feof here to make a difference between eof and io failure -- network
timeout for instance */
fclose(file);
return 0;
}
Since you are reading line-by-line, I suggest you restructure you file reading to match your logic
while (EOF != fscanf(file, "%[^\n]\n", line)) {
printf("> %s\n", line);
}
Is a way that one can read every line, one at a time. You can lookup the caveats of using fscanf and how to adjust the code to safely read without overflowing your line buffer.
Then, if you want to skip the first line, your code could look like this
if (EOF != fscanf(file, "%[^\n]\n", line)) {
// skip the first line
}
while (EOF != fscanf(file, "%[^\n]\n", line)) {
printf("> %s\n", line);
}
And your processing logic will look a lot like your mental process.
Yes, you could use a line counter, and only process if the counter is high enough; but, it is generally better to avoid introducing variables, if you can live without them. This is because an extra added variable doesn't make the code too hard to reason about; but, after you've repeated that "extra variable" rationale five or six times, the code quickly turns into something that's harder to maintain and harder to reason about. By the time you hit twenty or more extra variables, the odds of maintaining the code quickly without breaking it are lower.
Read the first line also with fgets() into a string and then scan the string for row, direction.
char line[256];
if (fgets(line, sizeof(line), file)) {
if (sscanf(line, "%d %c", &row, &direction) != 2) {
printf("Invalid first line '%s'\n", line);
} else {
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
}
}

Remove empty new line at the end of a file

I have a file that's storing strings from the user's input (stdin)
However there are 2 situations
If I read it normally, my file will have an empty line at its end due to the newline from the last string the user introduced.
If I remove the \n from the input string, the file stores all strings in the same line, which is not wanted.
How can I simply remove that newline from the end of the file?
I can edit and provide some of my code if required.
EDIT: Let's say the last line of a file I already have is "cards"
when the cursor is in front of "cards", if I press the down arrow it doesn't go on to the next line, while in this case it can happen once.
For my code to function perfectly I can't let that happen,
Here's an example of what I have:
f=fopen(somefile, "w");
do
{
fgets(hobby, 50, stdin);
fprintf(f, "%s", hobby)
} while(strcmp(hobby,"\n") != 0);
The newline character1 at the end of the file is part of the last line. Removing it is possible but makes the last line incomplete, which will break many programs. For example, concatenating another file at the end of this file will cause the last line to be merged with the first line of the concatenated file.
A Yuri Laguardia commented, if you later reopen this file in append mode to write more lines, the first one added would be concatenated at the end of this last incomplete line. Probably not the intended behavior.
If you do not want the file to contain empty lines, check user input before writing the line into the file:
void input_file_contents(FILE *fp) {
char userinput[80];
printf("enter file contents:\n");
while (fgets(userinput, sizeof userinput, stdin)) {
if (*userinput != '\n') {
fputs(userinput, fp);
}
}
}
EDIT:
Your code does not test for termination at the right place: you write the empty line before the test. Do not use a do / while loop:
f = fopen(somefile, "w");
if (f != NULL) {
/* read lines until end of file or empty line */
while (fgets(hobby, 50, stdin) != NULL && *hobby != '\n') {
fputs(hobby, f);
}
}
1 The newline character is actually a pair of bytes <CR><LF> on legacy systems.

Scanning from File, last string is repeating

I'm having some trouble with my code. I'm trying to read in some previous commands saved to a file, and place them in my array to use for later.
Here is my relevant piece of code:
if( (pastHist = fopen("history.txt", "r+")) == NULL)
{
pastHist = fopen("history.txt", "w+");
}
else
{
printf("%s", "INSIDE the else!");
pastHist = fopen("history.txt", "r+");
fscanf(pastHist, "%s", fstring);
while (fstring != NULL)
{
printf("%s %s", "the read in string is: ", fstring);
strcpy(cmndLine[cmndIndex], fstring);
strcpy(cmndLinecpy[cmndIndex], fstring);
cmndIndex++;
cmndNum++;
fscanf(pastHist, "%s", fstring);
}
}
Now the code writes to the file fine. (the writing part is held elsewhere). If I read from a file I wrote to before and the file said:
ls
rmdir angel
history
then i use this print statement to double check what i'm reading... it prints out
"INSIDE the else! the read in string is: lsthe read in string is: rmdirthe read in string is: angelthe read in string is: historythe read in string is: historythe read in string is: history
... and it repeats that the last thing read in was history a million times. Why is this the case? I also tried with the while condition
while(getchar() != EOF)
but that gave me the same thing.
please help.
Thanks.
fstring can never be set to NULL by that call to fscanf. What you want to check is the return value of fscanf.
Your getchar() loop likewise does nothing useful - it's reading from the standard input, not from your file.

append text at the end of a line in a file pure c code

I need to append a text at the end of each line in a file. I have the following code:
FILE *tmp_copy = tmpfile();
file = fopen ( argv[2], "rw" );
if ((file != NULL )) {
char line [ 128 ]; /* or other suitable maximum line size */
while( ( fgets ( line, sizeof line, file ) != NULL )) {
fputs(line, tmp_copy);
}
fclose ( file );
rewind(tmp);
char *p;
/* Reopen file now with write permissions */
fopen(argv[2], "w");
while (( p = fgets(line, 1024, tmp))!=NULL) {
//line[strlen(line)-1] = '\0'; /* Clear away newline */
//sprintf(line, "%s %s\n", line, more);
strcpy(line,"true");
//fputs(line, file);
}
fclose(file);
fclose(tmp);
}
}
I ve edited my code. still not working
but it's not working. why?
I'm guessing you want the line to be re-written to the file again. However, you are not writing to the file, just appending to the data in memory. It's also really not possible to read and write files at the same time like that, you have to do it in two steps:
Read from original file, appending wanted text to line, and write to temporary file
Rename (or copy) from temporary file to the original file
Edit: Pseudo-ish code for my answer:
original_file = fopen(original_file_name, "r");
temporary_file_name = tmpnam("dummy");
temporary_file = fopen(temporary_file_name, "w");
while (fgets(line, original_file))
{
remove_trailing_newline(line);
strcat(line, " TRUE\n");
fputs(line, temporary_file);
}
fclose(temporary_file);
fclose(original_file);
rename(temporary_file_name, original_file_name);
Unless the file is memory-mapped AND fgets returns a pointer to the original buffer (it doesn't. It needs to append the null.) AND strcat operates in place (it does), then you are not storing anything, only messing up some memory. Even if it did work, you would overwrite a part of the next line anyways.
You need to either
Write to a temporary file and rename that after you close the original one (as suggested by Joachim Pileborg).
Write into a buffer in memory and save it to the file when you are done reading.
Read the file into memory in one go, then start reading the buffer line by line.

C, reading a multiline text file

I know this is a dumb question, but how would I load data from a multiline text file?
while (!feof(in)) {
fscanf(in,"%s %s %s \n",string1,string2,string3);
}
^^This is how I load data from a single line, and it works fine. I just have no clue how to load the same data from the second and third lines.
Again, I realize this is probably a dumb question.
Edit: Problem not solved. I have no idea how to read text from a file that's not on the first line. How would I do this? Sorry for the stupid question.
Try something like:
/edited/
char line[512]; // or however large you think these lines will be
in = fopen ("multilinefile.txt", "rt"); /* open the file for reading */
/* "rt" means open the file for reading text */
int cur_line = 0;
while(fgets(line, 512, in) != NULL) {
if (cur_line == 2) { // 3rd line
/* get a line, up to 512 chars from in. done if NULL */
sscanf (line, "%s %s %s \n",string1,string2,string3);
// now you should store or manipulate those strings
break;
}
cur_line++;
}
fclose(in); /* close the file */
or maybe even...
char line[512];
in = fopen ("multilinefile.txt", "rt"); /* open the file for reading */
fgets(line, 512, in); // throw out line one
fgets(line, 512, in); // on line 2
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 2 is loaded into 'line'
// do stuff with line 2
fgets(line, 512, in); // on line 3
sscanf (line, "%s %s %s \n",string1,string2,string3); // line 3 is loaded into 'line'
// do stuff with line 3
fclose(in); // close file
Putting \n in a scanf format string has no different effect from a space. You should use fgets to get the line, then sscanf on the string itself.
This also allows for easier error recovery. If it were just a matter of matching the newline, you could use "%*[ \t]%*1[\n]" instead of " \n" at the end of the string. You should probably use %*[ \t] in place of all your spaces in that case, and check the return value from fscanf. Using fscanf directly on input is very difficult to get right (what happens if there are four words on a line? what happens if there are only two?) and I would recommend the fgets/sscanf solution.
Also, as Delan Azabani mentioned... it's not clear from this fragment whether you're not already doing so, but you have to either define space [e.g. in a large array or some dynamic structure with malloc] to store the entire dataset, or do all your processing inside the loop.
You should also be specifying how much space is available for each string in the format specifier. %s by itself in scanf is always a bug and may be a security vulnerability.
First off, you don't use feof() like that...it shows a probable Pascal background, either in your past or in your teacher's past.
For reading lines, you are best off using either POSIX 2008 (Linux) getline() or standard C fgets(). Either way, you try reading the line with the function, and stop when it indicates EOF:
while (fgets(buffer, sizeof(buffer), fp) != 0)
{
...use the line of data in buffer...
}
char *bufptr = 0;
size_t buflen = 0;
while (getline(&bufptr, &buflen, fp) != -1)
{
...use the line of data in bufptr...
}
free(bufptr);
To read multiple lines, you need to decide whether you need previous lines available as well. If not, a single string (character array) will do. If you need the previous lines, then you need to read into an array, possibly an array of dynamically allocated pointers.
Every time you call fscanf, it reads more values. The problem you have right now is that you're re-reading each line into the same variables, so in the end, the three variables have the last line's values. Try creating an array or other structure that can hold all the values you need.
The best way to do this is to use a two dimensional array and and just write each line into each element of the array. Here is an example reading from a .txt file of the poem Ozymandias:
int main() {
char line[15][255];
FILE * fpointer = fopen("ozymandias.txt", "rt");
for (int a = 0; a < 15; a++) {
fgets(line[a], 255, fpointer);
}
for (int b = 0; b < 15; b++) {
printf("%s", line[b]);
}
return 0;
This produces the poem output. Notice that the poem is 14 lines long, it is more difficult to print out a file whose length you do not know because reading a blank line will produce the output "x�oA". Another issue is if you check if the next line is null by writing
while (fgets(....) != NULL)) {
each line will be skipped. You could try going back a line each time to solve this but i think this solution is fine for all intents.
I have an even EASIER solution with no confusing snippets of puzzling methods (no offense to the above stated) here it is:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;//read the line
ifstream myfile ("MainMenu.txt"); // make sure to put this inside the project folder with all your .h and .cpp files
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Happy coding

Resources