Solved

read numbers from a file !

Posted on 1998-09-11
10
209 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 38
Why this code doesn't work? 8 108
XCode crashes while uploading to app store. 5 266
delete-remove 14 97
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

829 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