Debug asertion failed

This is a small experiment, it prints until pos is 508 on windows visual c++. and then apears the error

1
2
3
4
5
Debug asertion failed

File: fseek.c
Line: 149
Expression: str!=null


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <math.h>

void write_at(int pos);
int read_at(int pos);

int main ()
{

    char c;
    int f, pos, n;
    
    FILE * pFile;
    pFile = fopen("C:/a/exp.txt" , "w");

    for (f = 1; f<40001; f++) {
        fprintf(pFile, "0");

	if (!(f%200) && f<40000) {
		fprintf(pFile, "%c", '\n');
	}
    
    }

    fflush(pFile);
    fclose (pFile);

	
    for( n = 0; n<40000; n++) {
		 read_at(n);
    }	
    return 0;
}

void write_at(int pos) 
{
    FILE * pFile;
    pFile = fopen("C:/a/exp.txt" , "r+b");
    fseek (pFile , pos , SEEK_SET);
    fputs ( "1" , pFile );


    fflush(pFile);
    fclose ( pFile );
}

int read_at(int pos)
{
    FILE * pFile;
    int c;
    pFile = fopen("C:/a/exp.txt" , "r+b");

    fseek (pFile , pos , SEEK_SET);
    while ((c = fgetc(pFile)) != EOF) {
        printf("pos=%d \t %c\n", pos, c);
	return pos;	
    }

    fflush(pFile);
    fclose ( pFile );
    return 0;
}


How do i fix this?
How do i fix this?


You create the file in text mode. You read the file in binary mode.

In read_at you return without closing the file. Since you never check to see if the file is actually opened correctly you probably don't notice when you run out of file handles. Btw, why are you flushing a file opened for reading?
Ok, now is in text mode and read_at closes before is returning.

"Since you never check to see if the file is actually opened correctly you probably don't notice when you run out of file handles. "

So i must check if fopen returns null?

"Btw, why are you flushing a file opened for reading? "

Not anymore :D, but when writing it should right?

Now, one more problem, i'm writing 200 characters of '0' +'\n' on every line, but when i read from the file and print the output looks something like this

1
2
3
4
5
6
7
8
9
10
..............
pos=198   0
pos=199   0
pos=200

pos=201

pos=202   0
pos=203   0
................


on linux ( c language)


1
2
3
4
5
6
7
8
9
..............
pos=198   0
pos=199   0
pos=200

pos=201   0
pos=202   0
pos=203   0
................


Why?
Should i mark this as solved and make a new one with my last problem here or in windows section?
Seeking to arbitrary positions in a text file isn't recommended (as text input and output operations perform newline translations according to the operating system you're on.)

You should use binary mode if that's what you want to do.
Last edited on
Ok, but after i write to the file the 200 zeros +'\n' in the binary mode it doesn't writes the new line, just a weird character.



What if i write those lines in text mode and after i read/write to arbitrary positions in binary mode? What could go wrong?
Topic archived. No new replies allowed.