beginner level question

R

raam

hi all,
I trying to write an array of integers into a text
file.While writing into the file it works.I used fwrite function, but
when I read the same file using fread or fscanf it fails and it prints
some junk.I am not opening the file in binary mode, there are no
characters which will terminate the fscanf function operation.How to
write and read the integer array .
Thanks in advance.
 
R

raam

raam said:
hi all,
I trying to write an array of integers into a text
file.While writing into the file it works.I used fwrite function, but
when I read the same file using fread or fscanf it fails and it prints
some junk.I am not opening the file in binary mode, there are no
characters which will terminate the fscanf function operation.How to
write and read the integer array .
Thanks in advance.

a small addition to the above.
I am using DOS, Turbo c 2.0. in a win98 machine.
 
I

Ian Collins

raam said:
hi all,
I trying to write an array of integers into a text
file.While writing into the file it works.I used fwrite function, but
when I read the same file using fread or fscanf it fails and it prints
some junk.I am not opening the file in binary mode, there are no
characters which will terminate the fscanf function operation.How to
write and read the integer array .
Thanks in advance.
If you do not require the file to be binary, it might be easier for you
to write the file with fprintf and read it back with fscanf. Then you
will be able to check the data is written correctly by reading the file.

If you write binary data with fwrite, you should read it back with
fread, not fscanf. If you write a binary file, you should open it in
binary mode, otherwise characters such as line feed and null will be
misinterpreted.

For more help, post the code you are having problems with.
 
P

Peter Nilsson

raam said:
hi all,
I trying to write an array of integers into a text
file.While writing into the file it works.I used fwrite function, but
when I read the same file using fread or fscanf it fails and it prints
some junk.I am not opening the file in binary mode, there are no
characters which will terminate the fscanf function operation.How to
write and read the integer array .

Your problem is almost certainly on line 42.

Of course, we can give you a more accurate assessment and instruction
if you post the code that you're actually working on.

P.S. Have a look at...

http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction
 
R

raam

hi all,
thanks for the reply.
This is the code


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

main(){
FILE *p;
struct mm{
int a[100];
};
struct mm *n;
int i;


n = (struct mm *)malloc(sizeof(struct mm));
printf( "\n Press any key ");
getch();
for(i = 0 ; i<100 ; i++)
n->a= i;
p = fopen("example.txt","w");
for(i = 65;i<75;i++)
printf("%d, ",n->a);
getch();
fwrite(n,sizeof(struct mm),1,p);
fclose(p);

for(i = 0 ; i<100 ; i++)
n->a= 0;


p = fopen("example.txt","r");
fread(n,sizeof(struct mm),1,p);
fclose(p);

p = fopen("example1.txt","w");
fwrite(n,sizeof(struct mm),1,p);
fclose(p);

for(i = 65;i<75;i++)
printf("%d, ",n->a);

printf( "\n Press any key ");
getch();
free(n);
exit(0);
}
 
B

Barry Schwarz

hi all,
thanks for the reply.
This is the code


#include <stdio.h>
#include <conio.h>

A non-standard header may of us don't have and which you don't need.
#include <stdlib.h>

main(){

int main(void)
FILE *p;
struct mm{
int a[100];
};
struct mm *n;
int i;


n = (struct mm *)malloc(sizeof(struct mm));

Don't cast the return from malloc. It can never help and will allow
the compiler to suppress a diagnostic you would really want to see.

You should check calls to malloc for success.
printf( "\n Press any key ");
getch();

A non-standard function many of us don't have. If you change the
message to refer to the ENTER key, you could use the standard
getchar() function.
for(i = 0 ; i<100 ; i++)
n->a= i;
p = fopen("example.txt","w");


You should check calls to fopen for success.
for(i = 65;i<75;i++)
printf("%d, ",n->a);
getch();


How is the user supposed to know it is time to press a key to
continue?
fwrite(n,sizeof(struct mm),1,p);

This writes the binary structure to a file in text mode. Certain byte
values may special meaning to your operating system and be altered as
they are sent to the output device.
fclose(p);

for(i = 0 ; i<100 ; i++)
n->a= 0;


p = fopen("example.txt","r");
fread(n,sizeof(struct mm),1,p);


This attempts to read the binary structure from a file in text mode.
Certain byte values may special meaning to your operating system and
be altered as they are received from the output device or they may
cause the input operation to terminate.

You should definitely check calls to fread for success.
fclose(p);

p = fopen("example1.txt","w");
fwrite(n,sizeof(struct mm),1,p);
fclose(p);

for(i = 65;i<75;i++)
printf("%d, ",n->a);

printf( "\n Press any key ");
getch();
free(n);
exit(0);
}


You may have better success if you change all your calls to fopen to
specify binary mode instead of text mode.

It's not related to your problem but you don't need to enclose the
array in a structure.


Remove del for email
 
J

John Bode

raam said:
hi all,
thanks for the reply.
This is the code


#include <stdio.h>
#include <conio.h>

Be aware that this header is specific to the Microsoft platform, and is
not available everywhere.
#include <stdlib.h>

main(){

This line should be written as

int main(void) {

First of all, implicit typing (assuming a function returns int unless
otherwise specified) is a reliable source of errors, and second of all,
it's no longer supported in the latest version of the C standard.
FILE *p;
struct mm{
int a[100];
};
struct mm *n;
int i;


n = (struct mm *)malloc(sizeof(struct mm));

Unless you are working with a *very* old implementation (pre-C89),
don't cast the result of malloc().

First of all, malloc() returns a void*, which can be implicitly
converted to any other object pointer type. Second of all, if you
forget to #include <stdlib.h> or otherwise not have a prototype for
malloc() in scope, casting the result will prevent the compiler from
issuing a diagnostic, and you may experience some weird problems at
runtime.

Rewrite that line as

n = malloc(sizeof *n);

"sizeof *n" is synonymous with "sizeof(struct mm)", which is a little
easier to read, and if you ever change the type of n, you don't have to
make the corresponding change to the malloc() call.

*ALWAYS* check the result of malloc() before trying to use it.
printf( "\n Press any key ");
getch();

Again, be aware that any functions from the conio library are specific
to the Microsoft platform, and will not be universally available.
for(i = 0 ; i<100 ; i++)
n->a= i;
p = fopen("example.txt","w");


Since you're using fread() and fwrite(), you'll want to open the file
in binary mode, like so:

p = fopen("example.txt", "wb");

BTW, it's *always* a good idea to check that the return value of
fopen() isn't NULL, just in case the file could not be created.
for(i = 65;i<75;i++)
printf("%d, ",n->a);


In order to guarantee that the text shows up on the screen, either
print a newline or call fflush(stdout) at this point.
getch();
fwrite(n,sizeof(struct mm),1,p);

Again, you can replace sizeof(struct mm) with sizeof *n.
fclose(p);

for(i = 0 ; i<100 ; i++)
n->a= 0;


p = fopen("example.txt","r");


Same thing as above; replace "r" with "rb", and make sure to check the
result isn't NULL before continuing.
fread(n,sizeof(struct mm),1,p);
fclose(p);

p = fopen("example1.txt","w");
fwrite(n,sizeof(struct mm),1,p);
fclose(p);

for(i = 65;i<75;i++)
printf("%d, ",n->a);

printf( "\n Press any key ");
getch();
free(n);
exit(0);
}
 
K

Keith Thompson

Barry Schwarz said:
On 17 Jul 2006 00:23:54 -0700, "raam" <[email protected]>
wrote: [...]
printf( "\n Press any key ");
getch();

A non-standard function many of us don't have. If you change the
message to refer to the ENTER key, you could use the standard
getchar() function.

I think you're suggesting something like this:

printf("\n Press ENTER ");
fflush(stdout); /* I added this */
getchar(); /* ignore result */

The problem with this is that if the user types "foobar<ENTER>" rather
than just "<ENTER>", the call to getchar() will return 'f' and leave
the characters "oobar\n" in the input stream, to be read by further
input calls.

Here's a function the OP might find useful:

void skip_line(void)
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
/* do nothing */
}
}
 
R

raam

hi all,
I changed the extension from .txt to .dat and used only "wb"
and "rb" modes and now the program is working .I thought , whatever we
write the OS will dump it into the file but it seems that is not the
case and it depends on the file type.
Thanks for all your help.
Raam.
 
S

Simon Biber

raam said:
hi all,
I changed the extension from .txt to .dat and used only "wb"
and "rb" modes and now the program is working .I thought , whatever we
write the OS will dump it into the file but it seems that is not the
case and it depends on the file type.

It is the "rb" and "wb" that fixed your problem. Changing the extension
from ".txt" to ".dat" is a good idea, but it would not change what is
written to the file.
 

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,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top