Solved

read numbers from a file !

Posted on 1998-09-11
10
195 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
 

Author Comment

by:oualide
Comment Utility
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
Comment Utility
TO Sganta
---------------

How do you read a string from a file ? What is the equivalent to getc ? Thanks
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:oualide
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
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 opening and reading files in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now