What's wrong with my code?

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
ok here's a short snippet of code I was testing:

#include<stdio.h>

int main()
{
FILE *fin;
int fpos;

fin = fopen("testinput", "a");
fseek(fin, 10, SEEK_SET);
fprintf(fin, "%s", "**CURR**");

fclose(fin);
return(0);
}

testinput contains:
AB X 1984
AC X 1984
AA X 1984
AF X 1984
AE X 1984
AD X 1984

Shouldn't **CURR** be printing at position 10? For some reason the program always prints **CURR** at the very end (under AD X 1984). If there are any syntax errors, it's because I re-typed the code incorrectly (don't know hot to copy/paste through the terminal).
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Originally posted by: lousydood
Two things to try:

use mode "w" with fopen, not "a".

check for errors after calling fseek.

When I use "w", the entire gets overwritten for some reason. However, when I enter in the example code from cpluplus.com, the output I get matches their example. Hm..
 

lousydood

Member
Aug 1, 2005
158
0
0
That "some reason" is because "w" means "write starting at the beginning of the file" and "a" means "write starting at the end of the file."

What this sounds like is that fseek() is malfunctioning. Which is why I suggested that you check the error codes after fseek. Go read the man page for fseek to learn the possible errors.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Hm.. I just checked to make sure fseek was working and it is. Still getting **CURR** at the end of the file and not somewhere in the middle like I want it be.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
int i;
i = fseek();
if (i == 0)
{
//do whatever
}
else
{
printf("seek failed\n");
}
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Ok, I changed "a" to "w" and all I get now is garbage with **CURR** at the end. The stuff that's in the file dissappears T__T; I'm practically mimicking the sample code and it's still not working grr....
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
FILE *fopen( const char *filename, const char *mode );

mode:

"r" Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a" Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn?t exist.

"r+" Opens for both reading and writing. (The file must exist.)

"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+" Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn?t exist.

 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Well technically you've discovered a bug but it's explained away in the documentation.

ftell() before and after the fseek() reports the expected offsets but:

"When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten."

So the counter-intuitive "r+" should be used for overwriting all or part of an existing file.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Ok, well I changed "a" to "w" and I think the problem has to do with the fact that my file has multiple lines whereas sample codes only have 1 line in the output file. This shouldn't make a difference but my code isn't working and the sample is so..
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.

fopen() doesn't care if the file has one or a million text lines or is binary.

Are you serious about this?
 

aCynic2

Senior member
Apr 28, 2007
710
0
0
Well, here's what is ood about your code:

you open for appending.

SEEK_SET sets the file pointer to the beginning.

You then set the pointer to the beg+10.

Try using "r+".

That opens it for reading and writing, but the file MUST exist or the call fails.

If you need to determine if the file exists before opening, use _find or _findfirst.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: aCynic2
Well, here's what is ood about your code:

you open for appending.

SEEK_SET sets the file pointer to the beginning.

You then set the pointer to the beg+10.

Try using "r+".

That opens it for reading and writing, but the file MUST exist or the call fails.

If you need to determine if the file exists before opening, use _find or _findfirst.

fopen with read only flag will return an error if the file does not exist