473,395 Members | 2,468 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.

Reading a Text file

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

Apr 5 '06 #1
10 2744
nuke1872 schrieb:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 6 '06 #2
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]);

}

}

}

Apr 6 '06 #3
"nuke1872" <pa****@gmail.com> 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) 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.
Apr 6 '06 #4
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 Thompson wrote:
"nuke1872" <pa****@gmail.com> 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) 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.


Apr 6 '06 #5
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]);

}

}

}

Apr 6 '06 #6
"nuke1872" <pa****@gmail.com> writes:
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>.

--
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.
Apr 6 '06 #7
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);
}

Apr 7 '06 #8
"nuke1872" <pa****@gmail.com> writes:
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
<http://cfaj.freeshell.org/google/> to understand how and why.
#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 <stdlib.h>".)
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.

--
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.
Apr 7 '06 #9

Keith Thompson wrote:
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?)

Apr 7 '06 #10
"Bill Pursell" <bi**********@gmail.com> writes:
Keith Thompson wrote:
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?)


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.
--
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.
Apr 7 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.