I need to create a text file that looks like:
first line
second line
third line
forth line
fifth line
sixth line
I want to replace the third and forth lines with three new lines. The above contents would become:
first line
second line
new line1
new line2
new line3
fifth line
sixth line
How can I do this using c++?
never was taught file function in c++ and really need the help...Anything would be greatly appreciated thanks
Last edited on
You need to rewrite the file with the new output. Forget that seek stuff.
@kbw I can now open the file but how do I print its contents out...I have been trying but getting nowhere
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <stdio.h>
int main()
{
FILE * pFile;
char file[100];
if ((pFile = fopen ("Q4.txt", "r")) == NULL)
{
printf("File could not be opened\n");
}
int k;
for(k = 0; k <= 5; k++)
{
fscanf (pFile, "%s", &file[k]);
printf("%s\n", file[k]);
}
pFile = fopen ("Q4.txt","w");
if (pFile != NULL)
{
fputs ("first line\nsecond line\nthird line\nfourth line\nfifth line\n\n",pFile);
fclose (pFile);
}
}
|
Last edited on