Re: Reading a Text file
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 06 Apr 2006 22:09:14 GMT
"nuke1872" <pariub@xxxxxxxxx> writes:
Hi Michael ,
Thanks for the info and the welcome note. I followed your directions
and worked on it but cant execute it.
So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array [j][k].
0110000000000
0001000000000
0001000000000
0000110000000
0000001000000
0000001000000
0000000100000
0000000010000
0000000001000
0000000000110
0000000000001
0000000000001
0000000000000
#include <stdio.h>
main()
int main(void)
{
FILE *ifile; // input file pointer
FILE *ofile; //output file pointer
It's better to use "/* ... */" comments when posting here. "//"
comments are supported in C99, and as an extension by many C90
compilers, but they're still not 100% portable, and they can cause
problems with line-wrapping.
Also, consistent indentation is very helpful. There are a number of
different code formatting styles (and endless wars about which one is
"best"); you're not following any of them.
int i;
int j,k;
int itype[1][1];
char c;
ifile = fopen("numbers.txt","r");
ofile = fopen("stored in array","w");
You've created a file with spaces in its name. That's not necessarily
a problem, but consider whether that's really what you want to do.
In any case, you said earlier you want to store the data in an array,
not write it to a file.
i=0;
for(j=0;j<1000;j++)
{
for(k=0;k<1000;k++)
{
c = fgetc(ifile);
fgetc() returns an int; don't store its result in a char. You need to
use an int so you can distinguish the value of EOF. See
<http://www.c-faq.com/stdio/getcharc.html>.
if(c!=EOF) {
fscanf(ofile, "%c", itype[j][k]);
Now you're reading from your output file; that doesn't make any sense.
}
}
}
If you want to read data from an input file and store it in a matrix
(a 2-dimensional array), you only need to open one file. Read from
the file, and store the data in the array. Deciding how big the array
needs to be is likely to be the tricky part; section 7 of the FAQ,
<http://www.c-faq.com/>, is helpful here.
Also, please read <http://cfaj.freeshell.org/google/>.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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.
.
- Follow-Ups:
- Re: Reading a Text file
- From: nuke1872
- Re: Reading a Text file
- References:
- Reading a Text file
- From: nuke1872
- Re: Reading a Text file
- From: Michael Mair
- Re: Reading a Text file
- From: nuke1872
- Reading a Text file
- Prev by Date: This is My HW,PLS Help me!
- Next by Date: Re: This is My HW,PLS Help me!
- Previous by thread: Re: Reading a Text file
- Next by thread: Re: Reading a Text file
- Index(es):
Relevant Pages
|