Re: Reading a Text file



"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.
.



Relevant Pages

  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: socket communication: send & receive doesnt work right
    ... Maybe start from sending simple byte array to check what happen with byte ... I did a test of sending two doubles: ... public void send_doubles(double vals, int len) throws IOException ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Memory Allocation Problem, please help
    ... typedef struct word_tag{ ... array is not an array. ... static int total_word_count; ... static int word_index(const char *word); ...
    (comp.lang.c)