Reading a Text file

N

nuke1872

Hello guys,

I have a file names network.txt which contains a matrix.
I want to read this matrix as store it as an array.
I am new to stuff like these...can anybody help me out !!

Thanks
nuke
 
M

Michael Mair

nuke1872 said:
I have a file names network.txt which contains a matrix.
I want to read this matrix as store it as an array.
I am new to stuff like these...can anybody help me out !!

Well, welcome to comp.lang.c!
Just search for "read matrix from file" or similar on
groups.google.com in the comp.lang.c archives. If there remain
still questions or problems, try to write as much of the
programme or function as you can, then describe what you
expected and what you got or where there are general problems.

Have a look at the comp.lang.c FAQ
http://c-faq.com/
and do not miss the welcome page:
http://clc-wiki.net/wiki/Introduction_to_comp.lang.c


Cheers
Michael
 
N

nuke1872

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()

{

FILE *ifile; // input file pointer
FILE *ofile; //output file pointer
int i;
int j,k;
int itype[1][1];
char c;

ifile = fopen("numbers.txt","r");
ofile = fopen("stored in array","w");
i=0;
for(j=0;j<1000;j++)
{
for(k=0;k<1000;k++)
{
c = fgetc(ifile);
if(c!=EOF) {
fscanf(ofile, "%c", itype[j][k]);

}

}

}
 
K

Keith Thompson

nuke1872 said:
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
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/>.
 
N

nuke1872

Hello,

Thank you for the critique. I made the suggested changes. Also I am
assigning the array size as some large value ..here 100.

#include <stdio.h>

main()

{

FILE *ifile; // input file pointer
int i;
int j,k;
int itype[100][100];
int c;

ifile = fopen("numbers.txt","r");
ofile = fopen("stored in array","w");
i=0;
for(j=0;j<1000;j++)
{
for(k=0;k<1000;k++)
{
c = fgetc(ifile);
if(c!=EOF) {
fscanf(ifile, "%c", itype[j][k]);



}

}

}


Reply
Keith said:
nuke1872 said:
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
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/>.
 
N

nuke1872

Oops this one is the right code

#include <stdio.h>

main()

{

FILE *ifile; // input file pointer
int i;
int j,k;
int itype[100][100];
int c;

ifile = fopen("numbers.txt","r");
i=0;
for(j=0;j<1000;j++)
{
for(k=0;k<1000;k++)
{
c = fgetc(ifile);
if(c!=EOF) {
fscanf(ifile, "%c", itype[j][k]);

}

}

}
 
K

Keith Thompson

nuke1872 said:
Oops this one is the right code

#include <stdio.h>

main()

This should still be "int main(void)".
{

FILE *ifile; // input file pointer
int i;

You assign a value to i, but you never use it.
int j,k;
int itype[100][100];
int c;

ifile = fopen("numbers.txt","r");
i=0;
for(j=0;j<1000;j++)
{
for(k=0;k<1000;k++)
{

You're executing the inner loop one million times. Since you provide
no way to break out of it (even when you reach end-of-file), it *will*
be executed one million times.
c = fgetc(ifile);

This reads a single character from ifile.
if(c!=EOF) {
fscanf(ifile, "%c", itype[j][k]);

This also reads a single character from ifile; it's essentially
equivalent to
itype[j][k] = fgetc(ifile);

And you're not doing anything to recognize the end of a line.

So you've posted some code. I've provided some comments on it, but
you haven't asked any questions about it. What are you trying to
accomplish? How does the code's actual behavior differ from what you
expected?

Usually the best way to read text input is to read a full line at a
time using fgets() (*never* use gets()). You then have the line,
including the trailing '\n', in a string, which you can process as you
like. For now, make the input line "long enough"; later, you'll want
to worry about what happens if the input line is longer than what
you've allowed for.

Once again, please read <http://cfaj.freeshell.org/google/>.
Without context, it's impossible to tell what you're talking
about. Don't assume everybody has seen, or can easily find,
the article to which you're replying. Please also read
<http://www.caliburn.nl/topposting.html>.
 
N

nuke1872

Hi,

Lets put aside the previous code. I am trying this very simple code.
Please give your critique..


#include <stdio.h>

int main(){
int n,m;
double x[13][13];
FILE *inp;
inp = fopen("net.txt","r");
for (n = 0; n < 13; ++n) {
for (m = 0; m < 13; ++m)
fscanf (inp, "%lf", &x[n][m]);
}
fclose (inp);
return(0);
}
 
K

Keith Thompson

nuke1872 said:
Lets put aside the previous code. I am trying this very simple code.
Please give your critique..

Sure. Stop ignoring the advice I've been giving you.

Provide context when you post a followup. Read
#include <stdio.h>

int main(){

This is legal, but "int main(void)" is better.
int n,m;
double x[13][13];

13 is a "magic number", meaning that there's no indication in your
program of why you chose this particular value, or what it means. You
use 13 four times. If you decide to change it to 14, you'll have to
change it in four different locations.

I suggest adding something like "#define SIZE 13", and using SIZE
rather than a literal 13.
FILE *inp;
inp = fopen("net.txt","r");

And what happens if the fopen() call fails? (Answer: inp is set to a
null pointer, and the fscanf call invokes undefined behavior; when I
tried it, I got a segmentation fault.) Always check whether library
calls succeeded, even if your only response to a failure is to exit.
For example:

inp = fopen("net.txt", "r");
if (inp == NULL) {
fprintf(stderr, "Failed to open net.txt\n");
exit(EXIT_FAILURE);
}

(This requires a "#include said:
for (n = 0; n < 13; ++n) {
for (m = 0; m < 13; ++m)
fscanf (inp, "%lf", &x[n][m]);

This reads a double literal from inp, and stores the converted value
in x[n][m]. Ok, but what happens if there wasn't a double literal to
read from the input file? fscanf() returns a result; use it, and
decide how to respond if the result indicates that it failed.

Your program should work, but *only* if "net.txt" contains at least
169 floating-point literals (actually, fscanf with "%lf" also accepts
integer literals, converting them to floating-point). Don't assume
your input is correct; check it.
}
fclose (inp);
return(0);

A minor point: the parentheses are harmless, but unnecessary.
Personally, I prefer to avoid parentheses on a return statement; they
make it look too much like a function call, and the distinction is
important. But "return(0);" is perfectly legal, and some people even
prefer it.

You don't do anything with the values once you've read them. At least
for testing purposes, it would be good to write a loop that displays
the contents of your array.

*Please* indent your code. It's very difficult to read with
everything left-justified, and it will get much worse as your code
becomes more complex.
 
B

Bill Pursell

Keith said:
And what happens if the fopen() call fails? (Answer: inp is set to a
null pointer, and the fscanf call invokes undefined behavior; when I
tried it, I got a segmentation fault.) Always check whether library
calls succeeded, even if your only response to a failure is to exit.
For example:

inp = fopen("net.txt", "r");
if (inp == NULL) {
fprintf(stderr, "Failed to open net.txt\n");
exit(EXIT_FAILURE);
}


Keith, I'm curious to know why you don't use perror() here. Is there a
reason
to avoid perror(), or are you just simplifying for illustrative
purposes? (I think
my question here really is: is perror() fully portable?)
 
K

Keith Thompson

Bill Pursell said:
Keith, I'm curious to know why you don't use perror() here. Is
there a reason to avoid perror(), or are you just simplifying for
illustrative purposes? (I think my question here really is: is
perror() fully portable?)

Two reasons:

1. The standard doesn't say that fopen() sets errno on failure. If
you set errno to 0 before the call, and it's non-zero after the
call fails, it's *likely* to have a meaningful value, but it's not
guaranteed. It's also possible that fopen() could fail and not set
error; then you might get a message like "net.txt: No error".

2. I forgot about it.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top