473,320 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to read a ineteger Value from a text file in C?

My question is to calculate the mean of a comma separated dataset stored in a file. The program will be run on a system with no floating point library. Obey the following rules:

1.Maximum 100 values in dataset
2.Each data entry in the dataset is a byte (0 - 255)


I wrote the following code but when i execute it goes in a infinite loop,moreover I have anothe doubt Why might a system not have access to the floating point library?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  
  4. main(int argc,char *argv[])
  5. {
  6.     FILE *fp;
  7.     int ch=0;
  8.     int num=0,counter=0;
  9.     float mean=0;
  10.     if(argc !=2)
  11.     {
  12.             printf("\n You hv not entered the file name");
  13.             exit(1);
  14.     }         
  15.     if((fp=fopen(argv[1],"r"))== NULL)
  16.     {
  17.          printf("\n Cannot open the file");                        
  18.          exit(1);                        
  19.     }
  20.     while(!feof(fp))                     
  21.     {
  22.         fscanf(fp,"%d",&ch);             
  23.         if(ch>255)
  24.         {
  25.          printf("value out of range");
  26.         }           
  27.         if(ch==',')
  28.         {
  29.         printf("%d",ch);
  30.         }
  31.         else
  32.         {                              
  33.         printf("%d",ch);
  34.         num = num + ch;
  35.         counter++;
  36.         }
  37.     }                 
  38.     if(counter >100)
  39.     printf("\n mean cannot be calculated");
  40.     else
  41.     {
  42.     mean = num/counter;
  43.     printf("\n Mean is %f",mean); 
  44.     }
  45.     fclose(fp);
  46.     return ;
  47. }
my file has value as 1,2,3,4,5,6

please somebody help me out in this.
Feb 5 '08 #1
9 2785
My question is to calculate the mean of a comma separated dataset stored in a file. The program will be run on a system with no floating point library. Obey the following rules:

1.Maximum 100 values in dataset
2.Each data entry in the dataset is a byte (0 - 255)


I wrote the following code but when i execute it goes in a infinite loop,moreover I have anothe doubt Why might a system not have access to the floating point library?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  
  4. main(int argc,char *argv[])
  5. {
  6.     FILE *fp;
  7.     int ch=0;
  8.     int num=0,counter=0;
  9.     float mean=0;
  10.     if(argc !=2)
  11.     {
  12.             printf("\n You hv not entered the file name");
  13.             exit(1);
  14.     }         
  15.     if((fp=fopen(argv[1],"r"))== NULL)
  16.     {
  17.          printf("\n Cannot open the file");                        
  18.          exit(1);                        
  19.     }
  20.     while(!feof(fp))                     
  21.     {
  22.         fscanf(fp,"%d",&ch);             
  23.         if(ch>255)
  24.         {
  25.          printf("value out of range");
  26.         }           
  27.         if(ch==',')
  28.         {
  29.         printf("%d",ch);
  30.         }
  31.         else
  32.         {                              
  33.         printf("%d",ch);
  34.         num = num + ch;
  35.         counter++;
  36.         }
  37.     }                 
  38.     if(counter >100)
  39.     printf("\n mean cannot be calculated");
  40.     else
  41.     {
  42.     mean = num/counter;
  43.     printf("\n Mean is %f",mean); 
  44.     }
  45.     fclose(fp);
  46.     return ;
  47. }
  48.  
my file has value as 1,2,3,4,5,6

please somebody help me out in this.


Please somebody help me in this ....this is bit urgent....reply by today.
Feb 6 '08 #2
Hi Poojagupta,
I think your fscanf returns some failure status, otherwise there is no chance for looping. please check the returns status. i fell from "1,2,3,4,5,6" the "," is problem creater, so try with "1 2 3 4 5 6".

get back me , still if your facing the problem.

Regards,
Arul
Feb 6 '08 #3
"Why might a system not have access to the floating point library?"
i did not get you question, you mean folating point register ? .
Feb 6 '08 #4
sicarie
4,677 Expert Mod 4TB
You could also just getline() the line and use strtok() to parse the string.
Feb 6 '08 #5
Hello Sir,

Thanks for the quick reply ,but I have clearly mentione din the question that numbers have to be comma seperated so i cant use a dataset without them,Yes i knw "," is creating a problem but how to solve this problem that I dont know.:(

Please Help me out in this sir.I'll be grateful to you.

Hi Poojagupta,
I think your fscanf returns some failure status, otherwise there is no chance for looping. please check the returns status. i fell from "1,2,3,4,5,6" the "," is problem creater, so try with "1 2 3 4 5 6".

get back me , still if your facing the problem.

Regards,
Arul
Feb 6 '08 #6
Hello Sir,

Thanks for the quick reply ,but I have clearly mentione din the question that numbers have to be comma seperated so i cant use a dataset without them,Yes i knw "," is creating a problem but how to solve this problem that I dont know.:(

Please Help me out in this sir.I'll be grateful to you.
So, Sicarie advice would be better.
Feb 6 '08 #7
You could also just getline() the line and use strtok() to parse the string.
thanks sicarie for the advice ,it worked but with fgets and strtok,getline was not recognised by the compiler
Feb 6 '08 #8
Can you use the % operator?

Even if you can't, you can make a loop to get the remainder of your division and then making a fraction out of that with your divisor (e.g., sum is 13 and divisor is 3, so answer is 4 remainder 1, so to get the first decimal place 1/3 = x/10, x = 10/3 (remember you only need the first decimal place anyway, so an integer division will truncate), then just output your answer and decimal as part of the same printf.
Feb 6 '08 #9
Can you use the % operator?

Even if you can't, you can make a loop to get the remainder of your division and then making a fraction out of that with your divisor (e.g., sum is 13 and divisor is 3, so answer is 4 remainder 1, so to get the first decimal place 1/3 = x/10, x = 10/3 (remember you only need the first decimal place anyway, so an integer division will truncate), then just output your answer and decimal as part of the same printf.
Thanks every one for helping me in this situation.......
Feb 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
3
by: Ron | last post by:
I have a text file with the following format (pipe delimited) |column1=value|column2=value|column3=value|column4=value|... |column1=value|column2=value|column3=value|column4=value|... I have...
8
by: james | last post by:
I am trying to use Filestream to read a file ( .DAT) that contains values in HEX that I want to convert to text. I know the different offset addresses for each portion of the data I am trying to...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
1
by: ashok0866 | last post by:
I had created a macro to read data from an excel sheet and write the values to a text file. I had used "ActiveSheet.Range("GB" & k).Value" command to read the values from the excel. The issue...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
2
by: vighnesh | last post by:
Hi Folks Please let me know the difference between the following statements. Dim i as Integer Dim i as New Ineger As I tested both declares a variable 'i' of type integer intialized to...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.