472,103 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,103 software developers and data experts.

beginner level question

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.

Jul 17 '06 #1
9 1432

raam wrote:
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.

Jul 17 '06 #2
raam wrote:
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.

--
Ian Collins.
Jul 17 '06 #3
raam wrote:
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...c:Introduction

--
Peter

Jul 17 '06 #4
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]= i;
p = fopen("example.txt","w");
for(i = 65;i<75;i++)
printf("%d, ",n->a[i]);
getch();
fwrite(n,sizeof(struct mm),1,p);
fclose(p);

for(i = 0 ; i<100 ; i++)
n->a[i]= 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[i]);

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

Jul 17 '06 #5
On 17 Jul 2006 00:23:54 -0700, "raam" <mu***************@gmail.com>
wrote:
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]= i;
p = fopen("example.txt","w");
You should check calls to fopen for success.
> for(i = 65;i<75;i++)
printf("%d, ",n->a[i]);
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[i]= 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[i]);

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
Jul 17 '06 #6

raam wrote:
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.hor 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]= 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[i]);
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[i]= 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[i]);

printf( "\n Press any key ");
getch();
free(n);
exit(0);
}
Jul 17 '06 #7
Barry Schwarz <sc******@doezl.netwrites:
On 17 Jul 2006 00:23:54 -0700, "raam" <mu***************@gmail.com>
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 */
}
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 17 '06 #8
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.

Jul 18 '06 #9
raam wrote:
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.

--
Simon.
Jul 18 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard B. Kreckel | last post: by
16 posts views Thread by dario | last post: by
12 posts views Thread by Blaze | last post: by
6 posts views Thread by Alex | last post: by
18 posts views Thread by mitchellpal | last post: by
15 posts views Thread by Notre Poubelle | last post: by
8 posts views Thread by eggie5 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.