Helpful Information
 
 
Category: C Programming
how do you go through a text file and insert a string at the end of each line?

Hey everyone, sorry about the absurdly silly question, but I've been coding this project for weeks and The little things are escaping through the burnt hole in the back of my head.

I'm building a text file of columns of numbers. the first time I go through the text file I write the first two columns......it looks something like this:

1 0.0000
2 3.0000
3 14.0000
4 110.0000
5 146.0000
6 150.0000

....see what I mean? Once that's done (and not until that's done) I need to add another column.....then another, and another, etc.

SO....I need to add a string at the end of each line.
(such as " 134.0000")

The code I have here is totally wrong, I know, and I know why (because when you open a file in "r+", it appends, it won't insert)

any simple ideas.....?


if(j == 0)
{
i=0;
while(i<count)
{
i++;
sprintf(buffer, "%d", i);
fprintf(file2, " %s %s\n", buffer, aStringOfDouble);
}

fclose(file2);
}
else
{
file2 = fopen(ipname, "r+");

i=0;
while(i<count)
{
i++;

for(c=0;c<=j;c++)
{
fscanf(file2, "%s", buffer);
}

fprintf(file2, " %s", aStringOfDouble);
}

fclose(file2);
}
:p

hi,

i am just learning c++, but handled textfiles with perl a lot. If I got you right line 2 of your example should afterwards look something like:

2 3.0000 134.0000

?

I think you should read in the lines via getline().


include<iostream>
include<fstream>

string buffer;
ifstream in("file.txt");
//...

getline(in, buffer);



I don't know wether the filepointer is moved automatically after reading in the line, but you can modify the line and afterwards write it back.

This applies only if you use c++, of course, as fstream is a class, I think.

Hope it didn't do any harm...:p

thanks, but It has to be in C, not C++

Ok,

I dug through some references and found the following (still in search for the linewise attempt):

use fgets()!


char Data[80]; //for example, your buffer could take a different value

char *ReturnCode;
FILE *FilePointer;

ReturnCode=fgets(Data, 80, FilePointer);

ReturnCode == NULL when an error occours. The EOF is
considered to be an ERROR!


this way you could do:

while (fgets(line, sizeof(line), fp))

where line is a char-array of a certain size (255, e.g.) and fp is your filepointer. As fgets() returns NULL at the end of the file you read in all the lines with the while.

You could for example buffer the complete file in an twodimensional char-array and overwrite it afterwards or write your modified lines when still within the while by defining an output-filepointer to write to.

Not?


As I said, I am a beginner myself, but that looks fine to me.

Here is a very simple example on how you can add another column to your file.
For simplicity I have forgone all error checking.



#include <stdio.h>

int main(int argc, char **argv) {
FILE *fp;
FILE *tmp = tmpfile();
char *p;
char *more = "foo";
char line[1024];
/* First put contents of file into temporary file */
fp = fopen("file.txt", "r");
while ((p = fgets(line, 1024, fp))!=NULL) {
fputs(line, tmp);
}
fclose(fp);
rewind(tmp);
/* Reopen file now with write permissions */
fopen("file.txt", "w");
while (( p = fgets(line, 1024, tmp))!=NULL) {
line[strlen(line)-1] = '\0'; /* Clear away newline */
sprintf(line, "%s %s\n", line, more);
fputs(line, fp);
}
fclose(fp);
fclose(tmp);
return 0;
}


Hope this helps you.










privacy (GDPR)