There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
blue screen boot computer connection cpu crash css dell display driver drivers email error ethernet excel firefox firefox 3 game hard drive hardware internet internet explorer itunes laptop malware monitor network networking nvidia outlook outlook 2003 outlook express partition printer problem problems router security slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
FILE I/O question in C


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
soulfly's Avatar
Member with 48 posts.
 
Join Date: May 2004
Experience: Beginner
28-Oct-2005, 01:52 PM #1
FILE I/O question in C
Hello everyone.

I have a question regarding FILE I/O in c.
I have a text file that contains numbers
like this:
(---in the text file----)
1 22 4

what i want to do is to read the numbers in stored in that text file as "integer"
not as string and then use arithmetic operators (+, -, *, /) on those numbers. usually when I read numbers from a text file, it doesn't recognizes it as integer (it still treat the numbers from the text file as char).

here is the code that i've written

FILE *fp;
int a, b, c;
int i = 0;
int d;
int nums[100];
int num[100];
int temps[100];
if ((fp = fopen("new.txt", "r")) == NULL)
fprintf(stderr, "CANNOT OPEN FILE", "new.txt");
else
{
while ((fscanf(fp, "%d", nums)) != EOF);
{
fclose(fp);
fp = fopen("new.txt", "r");
while((nums[i] = fgetc(fp)) != EOF) {
if ((i %2) == 0)
{
temps[i] = nums[i];
}

i++;
}

}
}

what i want to know is how can i assign the value of temp[i] to an integer number. when i try to pass the value of temp[i] to an integer variable, the passed value from temp[i] to int is still read as character.
I did this program just to know how can i compute numbers that was read from a file.

any help is highly appreciated.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
28-Oct-2005, 03:43 PM #2
I'm not sure what you're trying to do.

But: If you're reading from a text file, and you want numbers, you will have to convert those characters and/or character strings to numbers (int, float, or double).

Your fscanf loop has a problem, in that it makes no allowance for white space between the numbers. Your fgetc() loop has a problem in that it reads one character at a time, not one number at a time. And, yes, a space character DOES have a numeric value, and the character '2' does not have the numeric value 2.

So, you must do two things: PARSE the numeric strings from the input, and CONVERT them to actual numeric datatypes.

OR, you can create a binary file to use as your input...
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:00 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.