Solved

read numbers from a file !

Posted on 1998-09-11
10
205 Views
Last Modified: 2010-04-15
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
Comment
Question by:oualide
10 Comments
 
LVL 4

Accepted Solution

by:
sganta earned 100 total points
ID: 1252657
Since this is in ASCII format.

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

Author Comment

by:oualide
ID: 1252658

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
 
LVL 84

Expert Comment

by:ozo
ID: 1252659
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:oualide
ID: 1252660
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
 

Author Comment

by:oualide
ID: 1252661
TO Sganta
---------------

How do you read a string from a file ? What is the equivalent to getc ? Thanks
0
 

Author Comment

by:oualide
ID: 1252662
TO Sganta
---------------

How do you read a string from a file ? What is the equivalent to getc ? Thanks
0
 
LVL 11

Expert Comment

by:alexo
ID: 1252663
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
 

Author Comment

by:oualide
ID: 1252664
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
 

Expert Comment

by:bertil
ID: 1252665
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
 

Author Comment

by:oualide
ID: 1252666
To bertil:

I dont understand what you are saying !
I still need to know
why use the inetger i here ?
why ==1 ?
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question