473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1508

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
16
by: dario | last post by:
Hi, Im new on phyton programming. On my GPRS modem with embedded Phyton 1.5.2+ version, I have to receive a string from serial port and after send this one enclosed in an e-mail. All OK if the...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
6
by: Alex | last post by:
Hello I am intersting in developing and my background is VBA used in Excel and a brief intro to Java. I am interested in learning beyond VB and feel that C++ would be a very good language to...
4
by: Yoram Biberman | last post by:
I have a few questions concerning concurrency control. I shall thank whoever can help me. Question #1 ========= Assume the following (concurrent) schedule, in which both transactions run in a...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
15
by: Notre Poubelle | last post by:
Hello, I have a large legacy MFC application. As is typical, there is an executable along with several MFC DLLs. One of these DLLs is created by staticly linking in many libraries resulting in...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
8
by: eggie5 | last post by:
I keep getting an error for line 7, what's wrong with this? from django.db import models class Poll(models.Model): question = models.CharField(max_length=200) pub_date =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.