• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

read numbers from a file !

I have a file that contains data like this:
1223 123 90 0 1234 1233 8888 3333 111 1 2222 1300
122 1 900......... . and I want to read it from a C program.

Can I read this file as numbers ? not character by character by using getc. I want to be able to read this file as 1223 in one go and then 123 then 90 etc. and as numbers not character by character or line by line. I appreciate your help. Thanks

0
oualide
Asked:
oualide
1 Solution
 
sgantaCommented:
Since this is in ASCII format.

First you have to read this as string by string
and convert into number using
"atoi" function
0
 
oualideAuthor Commented:

1/ No they are stored as decimal numbers. they are not in ASCII format.

2/ Is it possible to read the whole number (1223) as a string not character by character. Please clarify. Thanks

3/ If they are stored in the file as numbers, can I read them as int ?
Thanks
0
 
ozoCommented:
1/ What does it mean to be stored as a decimal number not in ASCII format?
Do you mean they're stored in EBCDIC?
2/ So you're saying the numbers are stored as strings?  Are they non-ASCII strings?
3/ Probably, depending on how the numbers are stored.
Assuming that the file looks exactly like your post, you can read it with

#include <stdio.h>
int i;
while( scanf("%d",&i) ){
    printf("%d\n",i);
}

(and if your file system is EBCDIC rather than ASCII, your stdio library probably will be too, so it should still work)
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
oualideAuthor Commented:
I dont know the file is as shown in my first message. It looks like numbers to me.Can I read them as numbers not characters
and is it possible to read the 1223 in one go ?

Thanks

TO OZO: Does the following read from a file or keyboard ?
#include <stdio.h>
int i;
while( scanf("%d",&i) ){
    printf("%d\n",i);
}

0
 
oualideAuthor Commented:
TO Sganta
---------------

How do you read a string from a file ? What is the equivalent to getc ? Thanks
0
 
oualideAuthor Commented:
TO Sganta
---------------

How do you read a string from a file ? What is the equivalent to getc ? Thanks
0
 
alexoCommented:
oualide, Ozo's solution is good but he made one small omission.
scanf() reads from the keyboard, fscanf() reads from a file.

Therefore:
    #include <stdio.h>
    int i;
    FILE* pFile = fopen( /* ... */ );
    while (fscanf(pFile, "%d", &i) == 1)
    {
        printf("%d\n", i);
    }

>> How do you read a string from a file?

There are several options.  You can use fscanf() with the "%s" format (but then, why don't you read the integer directly?).  You can also read a complete line with fgets() function.

However, both these solutions are inferior to calling fscanf() with "%d".
0
 
oualideAuthor Commented:
Ok, thanks alot but 2 things I dont know here:
what is i here ?
why == 1 ?

    int i;
    FILE* pFile = fopen( /* ... */ );
    while (fscanf(pFile, "%d", &i) == 1)
    {
        printf("%d\n", i);
    }

Thanks
0
 
bertilCommented:
Hmmm.. i is as you can see an integer, replace 'int i' & the other i's with whathever you want.
If you call fscanf(pFile," %d %d",&val1,&val2) & if succeds it
will return 2 i.e the number of succeded conversions, so it the
example to you it will return 1 since there id only 1 value to
convert. Got It!

0
 
oualideAuthor Commented:
To bertil:

I dont understand what you are saying !
I still need to know
why use the inetger i here ?
why ==1 ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now