File management in C

R

Ritesh

Hi!!!!!!!!!!!
I am a final year engineering student. I am making a final year
project on reed-solomon codec in C using VC++ 6.0. The input to the
program can be any file that the user wants to encode and then later
decode.

This is where I have a problem. I am using fopen, fscanf and fprintf
for input output purposes. I know these are the simple and std means
of doing so. Unfortunately when I run the program the file pointer for
the output stream (sometimes) start printing a few junk characters
(any where in the middle of the file) and then jumps back many places
and starts printing from there. When this error occurs it prints a few
lines and then stops printing completely.

I am NOT USING ANY function in my program that makes the file pointer
jump back.

Can anyone please tell me how I can solve this problem? Your advice
will be highly appreciated. Thank you for your time.

Sincerely,

Ritesh
 
K

Keith Thompson

This is where I have a problem. I am using fopen, fscanf and fprintf
for input output purposes. I know these are the simple and std means
of doing so. Unfortunately when I run the program the file pointer for
the output stream (sometimes) start printing a few junk characters
(any where in the middle of the file) and then jumps back many places
and starts printing from there. When this error occurs it prints a few
lines and then stops printing completely.

I am NOT USING ANY function in my program that makes the file pointer
jump back.

What do you mean by "file pointer"?

Do you mean the cursor (on a terminal)? If so, you're probably
printing non-printable characters, including control characters that
cause the cursor to move around.

If you really mean that it's doing the equivalent of fseek(), how do
you know it's doing so? What behavior are you seeing?
 
L

Lawrence Kirby

Hi!!!!!!!!!!!
I am a final year engineering student. I am making a final year
project on reed-solomon codec in C using VC++ 6.0. The input to the
program can be any file that the user wants to encode and then later
decode.

It sounds like your input and possibly output will be general binary data.
This is where I have a problem. I am using fopen, fscanf and fprintf
for input output purposes.

For binary data you would normally use fread() and fwrite(), fscanf() and
fprintf() are really for text data.
I know these are the simple and std means
of doing so. Unfortunately when I run the program the file pointer for
the output stream (sometimes) start printing a few junk characters
(any where in the middle of the file) and then jumps back many places
and starts printing from there. When this error occurs it prints a few
lines and then stops printing completely.

Standard fscanf() and fprintf() functions can't reposition backwards in
the file. Either something else in the program is doing this or you are
misinterpreting what you are seeing.

What is the file format you are producing? If it is basic binary then
"junk characters" aren't an issue because any character is valid. If it
has a specific format and the program is generating incorrect output then
you have a bug in your program. If you want us to help with that you'll
need to show us the code.
I am NOT USING ANY function in my program that makes the file pointer
jump back.

Then you need to do more research into what is actually happening, maybe
reduce the program to something minimal that reproduces the problem. That
might be something you could post.

Lawrence
 
J

Jason Curl

Ritesh said:
Hi!!!!!!!!!!!
I am a final year engineering student. I am making a final year
project on reed-solomon codec in C using VC++ 6.0. The input to the
program can be any file that the user wants to encode and then later
decode.

This is where I have a problem. I am using fopen, fscanf and fprintf
for input output purposes. I know these are the simple and std means
of doing so. Unfortunately when I run the program the file pointer for
the output stream (sometimes) start printing a few junk characters
(any where in the middle of the file) and then jumps back many places
and starts printing from there. When this error occurs it prints a few
lines and then stops printing completely.

I am NOT USING ANY function in my program that makes the file pointer
jump back.

Can anyone please tell me how I can solve this problem? Your advice
will be highly appreciated. Thank you for your time.

Can you recreate the problem with a small piece of code (wholly
contained and compilable) and post it to the newsgroup? Or I'm afraid
that many of the skilfull here will just say there are too many
speculations to just help.
 
O

osmium

"Ritesh" swrites:
I am a final year engineering student. I am making a final year
project on reed-solomon codec in C using VC++ 6.0. The input to the
program can be any file that the user wants to encode and then later
decode.

<snip>
"*Any* file" means the file must be opened in binary mode. Is that how you
opened it? It seems a fair guess that you did not.
 
C

CBFalconer

Ritesh said:
I am a final year engineering student. I am making a final year
project on reed-solomon codec in C using VC++ 6.0.

If that matters you are off-topic here. However it is probably
immaterial.
The input to the program can be any file that the user wants to
encode and then later decode.

This is where I have a problem. I am using fopen, fscanf and
fprintf for input output purposes. I know these are the simple and
std means of doing so. Unfortunately when I run the program the
file pointer for the output stream (sometimes) start printing a
few junk characters (any where in the middle of the file) and then
jumps back many places and starts printing from there. When this
error occurs it prints a few lines and then stops printing
completely.

I am NOT USING ANY function in my program that makes the file
pointer jump back.

Yes you are, or it wouldn't be doing it. It smells like a wild
pointer somewhere, or other means of invoking undefined behaviour.
Lacking the source, we can help no further.

You should reduce the program to a minimal compilable unit that
demonstrates the problem. Not over about 100 lines. Publish that
here if you haven't already found your mistake.

Since you are using the broken google interface, pay close
attention to my sig lines, below. Then trim the quotes to the
relevant portion, and do not top-post.
 
R

Ritesh

Hi!!!!!!
THANK YOU EVERYONE for making me realize that there is indeed an
problem with my program and not with the file pointers. I am glad to
inform you that I have found the problem and fixed it.

Initially I was opening the file in Binary Mode but the syntax was as
follows:

stream = fopen( "encoded.enc", "r , b" )

With this syntax the compiler was not stating any syntax error but the
file was always opened in Text Mode. Then I changed the syntax as
follows:

stream = fopen( "encoded.enc", "r""b" )

(Note the change from "r , b" to "r" "b" )
Complier accepted this syntax too and this time the file opened in
Binary Mode and the problem was fixed.

Again, Thank you.

Sincerely,

Ritesh


CBFalconer said:
If that matters you are off-topic here. However it is probably
immaterial.


Yes you are, or it wouldn't be doing it. It smells like a wild
pointer somewhere, or other means of invoking undefined behaviour.
Lacking the source, we can help no further.

You should reduce the program to a minimal compilable unit that
demonstrates the problem. Not over about 100 lines. Publish that
here if you haven't already found your mistake.

Since you are using the broken google interface, pay close
attention to my sig lines, below. Then trim the quotes to the
relevant portion, and do not top-post.

Hi!!!!!!
THANK YOU EVERYONE for making me realize that there is indeed an
problem with my program and not with the file pointers. I am glad to
inform you that I have found the problem and fixed it.

Initially I opening the file in Binary Mode but the syntax was as
follows:

stream = fopen( "encoded.enc", "r , b" )

With this syntax the compiler was not stating any syntax error but the
file was always opened in Text Mode. Then I changed the syntax as
follows:

stream = fopen( "encoded.enc", "r""b" )

(Note the change from "r , b" to "r" "b" )
Complier accepted this syntax too and this time the file opened in
Binary Mode and the problem was fixed.

Again, Thank you.

Sincerely,

Ritesh
 
K

Keith Thompson

Initially I was opening the file in Binary Mode but the syntax was as
follows:

stream = fopen( "encoded.enc", "r , b" )

With this syntax the compiler was not stating any syntax error but the
file was always opened in Text Mode. Then I changed the syntax as
follows:

stream = fopen( "encoded.enc", "r""b" )

That will work, but it's an odd way to write it.
"r""b"
is exactly equivalent to
"rb"
which is much clearer. The form you're using is two adjacent string
literals, which are concatenated to together at compilation time to
form a single string "rb".

And please don't top-post. Your response belongs after, or
interspersed with, any quoted text, and you should snip anything
that's not relevant. (For some reason, your response appeared both
before and after the quoted text.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top