Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about read data from a text file.

Reply
Thread Tools

Question about read data from a text file.

 
 
bowlderster
Guest
Posts: n/a
 
      04-04-2007
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.


 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      04-04-2007
"bowlderster" <> ha scritto nel messaggio
news:...
[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.h> for 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.
>
>



 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      04-04-2007
"bowlderster" <> ha scritto nel messaggio
news:...
[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.h> for 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



 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-04-2007
In article <>,
bowlderster <> wrote:
>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
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      04-04-2007
On Apr 4, 3:20 pm, bowlderster <bowlders...@gmail.com> 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:
>
> #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

 
Reply With Quote
 
bowlderster
Guest
Posts: n/a
 
      04-04-2007
"Army1987" <> writes:

> "bowlderster" <> ha scritto nel messaggio
> news:...
> [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.h> for 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.
 
Reply With Quote
 
bowlderster
Guest
Posts: n/a
 
      04-04-2007
(Walter Roberson) writes:

> In article <>,
> bowlderster <> wrote:
>>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.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read a ruby script like you would read a text file Mmcolli00 Mom Ruby 2 01-27-2009 10:52 PM
How to read from a file, stored the data in an array, and return number of records read back to main tyeung168@gmail.com C++ 2 02-27-2007 07:12 AM
Is the ability to read Hex data like learning to read the matrix? kemp MCSE 2 10-13-2006 03:58 PM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM
Re: Unable to read video DVDs and can read Data DVDs Biz DVD Video 0 07-22-2005 03:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57