473,378 Members | 1,134 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,378 software developers and data experts.

Question about read data from a text file.

Hello, all.

This is the text file named test.txt.

1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

I wanna to read the data to an array, as the follows:

#include <stdio.h>
#define MAX 30
int main()
{
int i = 0 ;
FILE *fp ;
int a[30] = {0};
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
for(i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a[i]) ;
printf("%i\t",a[i]);
}
fclose(fp);
return 0;
}

It is successful.

And I change the type of the array to float
....
float a[30];
....
fscanf(fp,"%f",&a[i]);
printf("%f\t",a[i]);
....

It works as well.

But when I change the type of the array to double
....
double a[30];
....
fscanf(fp,"%g",&a[i]);
printf(fp,"%g",a[i]);
....

It cannot work. It seams that the array is zero.

I wanna to know the reason and how can read the data to a double array.This is one question.

Another is that:
If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.

I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?
If it can work, please give me your example for the above file.
I am learning the language at the beginning. Any encouragement will be helpful.
Apr 4 '07 #1
7 3003
"bowlderster" <bo*********@gmail.comha scritto nel messaggio
news:87************@gmail.com...
[snip]
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
If I redirect the output to a file, I won't be able to read the error until
I read that file.
You must write to stderr, not stdin. If you use perror() it will also add a
colon, a space, an explaination of the error, and a newline char.
perror("Open file fail");
Also, if the test fails, the rest of the program will run as if fp wasn't
NULL.
use exit(EXIT_FAILURE); (you must #include <stdlib.hfor these).
for(i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a[i]) ;
printf("%i\t",a[i]);
}
fclose(fp);
return 0;
}

It is successful.

And I change the type of the array to float
...
float a[30];
...
fscanf(fp,"%f",&a[i]);
printf("%f\t",a[i]);
...

It works as well.

But when I change the type of the array to double
...
double a[30];
...
fscanf(fp,"%g",&a[i]);
printf(fp,"%g",a[i]);
...

It cannot work. It seams that the array is zero.

I wanna to know the reason and how can read the data to a double
array.This is one question.

Another is that:
If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

how to read the file directly to the 3rd line? I mean that I just wanna to
read the numbers.

I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?
If it can work, please give me your example for the above file.
I am learning the language at the beginning. Any encouragement will be
helpful.


Apr 4 '07 #2
"bowlderster" <bo*********@gmail.comha scritto nel messaggio
news:87************@gmail.com...
[snip]
>int a[30] = {0};
You meant a[MAX]. If you change MAX at the beginning and forget to change 30
here, that could be a problem.
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
If I redirect the output to a file, I won't be able to read the error until
I read that file.
You must write to stderr, not stdin. If you use perror() it will also add a
colon, a space, an explaination of the error, and a newline char.
perror("Open file fail");
Also, if the test fails, the rest of the program will run as if fp wasn't
NULL.
use exit(EXIT_FAILURE); (you must #include <stdlib.hfor these), or even
just return EXIT_FAILURE as you are in main()
But when I change the type of the array to double
...
double a[30];
...
fscanf(fp,"%g",&a[i]);
printf(fp,"%g",a[i]);
...

It cannot work. It seams that the array is zero.
http://c-faq.com/stdio/scanf2.html

Apr 4 '07 #3
In article <87************@gmail.com>,
bowlderster <bo*********@gmail.comwrote:
>If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
>how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.
>I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?
You cannot do that. As the local man page says for fseek()

The ANSI C Standard restricts the use of offsets, when stream refers to a
text file. When operating on these files, the value of offset must be
zero unless whence is SEEK_SET. This restriction is necessary as the
unit of offsets may not be bytes on some systems, and arithmetic may not
meaningfully be performed on them.

And if you are using SEEK_SET on a text stream, then the value
you pass in must be the result of an ftell(), and ftell() on
a text stream theoretically returns an opaque value, not an
integral offset value.

Text files are not necessarily linearly structured (e.g., on the
VMS operating system), and even on systems that do structure text
files linearly, there is no way to query what the line termination
sequence is. (You can also run into issues such as the possibility
that the OS might consider as a valid line termination one or more
carriage returns before a linefeed; the first line might happen to
end CR LF internally, but the second line might happen to end
CR CR CR CR LF internally, so you cannot assign any fixed
predictable size for line termination offset calculations.)
If you were willing to lose portability and only deal with
systems that did use linear text files, and on which you knew
that the line termination was always the same size, then you
could read the file as a binary file and calculate byte offsets
if you knew the exact length of the first and second lines ahead
of time. But there is no portable way to fseek() to any given
line in a text stream -- not that doesn't involve reading
the file through once to find all the line offsets.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Apr 4 '07 #4
On Apr 4, 3:20 pm, bowlderster <bowlders...@gmail.comwrote:
This is the text file named test.txt.

1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

I wanna to read the data to an array, as the follows:

#include <stdio.h>
#define MAX 30
int main()
{
int i = 0 ;
FILE *fp ;
int a[30] = {0};
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
for(i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a[i]) ;
printf("%i\t",a[i]);}

fclose(fp);
return 0;

}

But when I change the type of the array to double
...
double a[30];
...
fscanf(fp,"%g",&a[i]);
printf(fp,"%g",a[i]);
...

It cannot work. It seams that the array is zero.
fscanf(fp, "%lf", &a[i]) ;
Another is that:
If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.
You could use fgets (twice) to read each of the first two lines.

Michael

Apr 4 '07 #5
"Army1987" <pl********@for.itwrites:
"bowlderster" <bo*********@gmail.comha scritto nel messaggio
news:87************@gmail.com...
[snip]
>>int a[30] = {0};
You meant a[MAX]. If you change MAX at the beginning and forget to change 30
here, that could be a problem.
Thanks.I just want to test.
>
>if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
If I redirect the output to a file, I won't be able to read the error until
I read that file.
You must write to stderr, not stdin. If you use perror() it will also add a
colon, a space, an explaination of the error, and a newline char.
perror("Open file fail");
Also, if the test fails, the rest of the program will run as if fp wasn't
NULL.
use exit(EXIT_FAILURE); (you must #include <stdlib.hfor these), or even
just return EXIT_FAILURE as you are in main()
>But when I change the type of the array to double
...
double a[30];
...
fscanf(fp,"%g",&a[i]);
printf(fp,"%g",a[i]);
...

It cannot work. It seams that the array is zero.
http://c-faq.com/stdio/scanf2.html
This is helpful.

Thank you.
Apr 4 '07 #6
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <87************@gmail.com>,
bowlderster <bo*********@gmail.comwrote:
>>If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
>>how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.
>>I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?

You cannot do that. As the local man page says for fseek()

The ANSI C Standard restricts the use of offsets, when stream refers to a
text file. When operating on these files, the value of offset must be
zero unless whence is SEEK_SET. This restriction is necessary as the
unit of offsets may not be bytes on some systems, and arithmetic may not
meaningfully be performed on them.

And if you are using SEEK_SET on a text stream, then the value
you pass in must be the result of an ftell(), and ftell() on
a text stream theoretically returns an opaque value, not an
integral offset value.

Text files are not necessarily linearly structured (e.g., on the
VMS operating system), and even on systems that do structure text
files linearly, there is no way to query what the line termination
sequence is. (You can also run into issues such as the possibility
that the OS might consider as a valid line termination one or more
carriage returns before a linefeed; the first line might happen to
end CR LF internally, but the second line might happen to end
CR CR CR CR LF internally, so you cannot assign any fixed
predictable size for line termination offset calculations.)
If you were willing to lose portability and only deal with
systems that did use linear text files, and on which you knew
that the line termination was always the same size, then you
could read the file as a binary file and calculate byte offsets
if you knew the exact length of the first and second lines ahead
of time. But there is no portable way to fseek() to any given
line in a text stream -- not that doesn't involve reading
the file through once to find all the line offsets.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
I know a lot from your words.

Apr 5 '07 #7
bowlderster wrote:
This is the text file named test.txt.

1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895

I wanna to read the data to an array, as the follows:
"want to" standard english is easier to read.

code is more readable if indented and with appropriate whitespace
#include <stdio.h>
#define MAX 30

int main()
int main (void)
is better style
{
int i = 0 ;
FILE *fp ;
int a[30] = {0};

if ( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");

for (i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a[i]) ;
always check the return value of fscanf()

printf("%i\t",a[i]);
}

fclose(fp);
return 0;
}
<snip>
I am learning the language at the beginning. Any encouragement will be helpful.
not bad for a beginner
--
Nick Keighley

Apr 5 '07 #8

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

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
5
by: Darrel | last post by:
I thought this warranted a new thread. Yesterday I asked about access relatively static content...is it better to read from the DB, or just grab a text file. It was suggested that I use the DB...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
10
by: Avi | last post by:
Hi I need to read in a large set of text files (9GB+ each) into a database table based on fixed width lengths. There are several ways to complete this, but I am wondering if anyone has...
1
by: sharpy | last post by:
i hope this is a simple answered question this program has been annoying me for some time, its pretty much the first thing i have attempted to do in perl. its a pretty simple guestbook type thing....
6
by: kath | last post by:
Hi all, Platform: winxp Version: Python 2.3 I have a task of reading files in a folder and creating an one excel file with sheets, one sheet per file, with sheet named...
0
by: Smoke | last post by:
Im developing a custom app that has to read a file generated from other program, actually, the file is pretty much a mail message, which contains the subject, to, from, body, etc etc and also the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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...

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.