Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing into a file error

Status
Not open for further replies.

rkv19

Programmer
Jun 13, 2008
5
US
#include<stdio.h>
Hi,
I would like to know why i am getting a segmentation fault when i run this code, also i want to write the statement as it is into file.txt just changing the value of m in m.dat without changing the format which i am not getting .Can someone help me with this??

main()
{
FILE * fp;
int i=1;
fp = fopen ( "file.txt","w");
for(i=1;i<10;i++){
fputs("c:\My Documents\Folder1\Input1\m.dat xyz.dat\n",fp);
fseek(fp,26,SEEK_SET);
fputc(i,fp);
}
fclose(fp);
}
 
Use [ignore]
Code:
[/ignore] tags around your code.

There are many problems with that code.
- main() MUST return an int. Anything else is an error and non-portable.
- You aren't checking the return value of fopen() to see if there was an error opening the file.
- You are using single backslashes in your strings, which is an escape character. You need to use "\\" if you actually want a "\" in your string.
- In your fseek() statement, I'm not sure what you're trying to do? You keep going to the 26th character in the file, then calling fputc() to write the ASCII value of i in that place. I'm pretty sure that's not what you intended?

Also, this looks like C code, not C++.
If you have questions specific to C, there is a C forum here.
 
This is what i need to print in my output file

c:\My Documents\Folder1\Input1\1.dat xyz.dat
c:\My Documents\Folder1\Input1\2.dat xyz.dat
....
.
.
.
.
c:\My Documents\Folder1\Input1\10.dat xyz.dat

My output file should look exactly like this. even if i am trying to use \\ (double slash ) still its nt printing that !!

Thanks for the reply !!
 
fprintf() should be a lot easier:
Code:
fprintf( fp, "c:\\My Documents\\Folder1\\Input1\\[COLOR=red][b]%d[/b][/color].dat xyz.dat\n", i );
Then get rid of the fseek() & fputc().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top