Reading data file in C

Reply

Join Date: Apr 2008
Posts: 27
Reputation: Afi83 is an unknown quantity at this point 
Solved Threads: 0
Afi83's Avatar
Afi83 Afi83 is offline Offline
Light Poster

Reading data file in C

 
0
  #1
Apr 14th, 2008
Hi there
I am really new in C programming (about 3 weeks). Before I begin my question I have to excuse because of my bad English.
My question is on reading a file that contains double and integer type numbers.
For reading file I use fgets() that read a line and save it to an array ( I define the string array as: char [100][150]).
But know how I can access to my values in array. In array I have each line of my data as string.
My other codes are based on it.
I have to say that, my file is exported from excel as text file and all data separated by space.

Yours help truly appreciated,
Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading data file in C

 
0
  #2
Apr 14th, 2008
>>But know how I can access to my values in array
Is that the question -- how to extract each of the numbers in the string ? extract them using sscanf(). There is a simple example
  1. int main()
  2. {
  3. char line[] = "123 234.56 789";
  4. int a, b;
  5. float c;
  6. sscanf(line,"%d %f %d", &a, &c, &b);
  7. printf("%d %f %d", a, c, b);
  8. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Reading data file in C

 
0
  #3
Apr 14th, 2008
question:

does each line of your data always have the same number of INTs and FLOATs, and are they always found in the same order?

if so, Dragon's suggestion for "sscanf()" is perfect. if not, you will need a more complex routine, perhaps using "strtok()"
Last edited by jephthah; Apr 14th, 2008 at 3:12 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: Afi83 is an unknown quantity at this point 
Solved Threads: 0
Afi83's Avatar
Afi83 Afi83 is offline Offline
Light Poster

Re: Reading data file in C

 
0
  #4
Apr 14th, 2008
Hi
Thanks for your attention.
All I have done is this code that made error. And numbers don’t save in new array. Only my and zeros print on console.
Also in my file I know only the row of integer value.

#include <stdio.h>

int main(void)
{
      
   char input[150][350];
   char line[350];
   int i,j=0;
   double num[1000];  /*But i don't Know the exact num*/
   FILE *get_input;
   get_input=fopen("input.inp","r");
   
   if (get_input!=NULL)
   {
       while (fgets(line,sizeof line,get_input)!=NULL)
       {
             strcpy(input[i],line);
             printf("My Inputs%s\n",input[i]);
             i++;
       }
   }
   else
   {
       perror("File cannot be found");
   }
   i=0;
   while (line[i]!=EOF)
   {
         sscanf(input[i],"%f",&num[i]);
         printf("My Numeric Array%f",num[i]); 
         /*Only print text and zeros finally because of array size an error*/
         i++;
   }
   
   system("pause");
   return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading data file in C

 
0
  #5
Apr 14th, 2008
>>strcpy(input[i],line);
What's the initial value of i? Answer: undefined because you didn't set it to 0 before that loop started.

>> while (line[i]!=EOF)
line[i] will NEVER EVER be EOF. So that's the wrong way to code that loop. What you need to do is create another integer that counts the number of lines read so that in this loop you can check the number of lines. You have already done that to an extent -- in the loop that reads the data you are using variable i but then the program uses that same variable over again. Use a different counter.
for(i = 0; i < counter; i++)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 27
Reputation: Afi83 is an unknown quantity at this point 
Solved Threads: 0
Afi83's Avatar
Afi83 Afi83 is offline Offline
Light Poster

Re: Reading data file in C

 
0
  #6
Apr 14th, 2008
Ok,
I think I have another mistake in second loop. Because I send each line to input[] array but in second loop I use line[] array for getting number from each line. I correct this as bellow but encounter an error that you can't compare pointer and integer. I think that the arrays are pointer but what is this error source.
What I have to do to solve these.

Thank you,
  1. while (input[i]!=EOF)
  2. {
  3. sscanf(input[i],"%f",&num[i]);
  4. printf("My Numeric Array%f",num[i]);
  5.  
  6. i++;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading data file in C

 
0
  #7
Apr 14th, 2008
In order to learn how to program you first need to learn how to read. Please read my previous very very carefully and you will find the answer to your problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: tah is an unknown quantity at this point 
Solved Threads: 0
tah tah is offline Offline
Newbie Poster

Re: Reading data file in C

 
0
  #8
Apr 14th, 2008
HI Sir
I wish to write a program that can do the following


Write a program to count and print the total number of:

upper case letters, i.e. ‘A’ to ‘Z’
lower case letters, i.e. ‘a’ to ‘z’
digits, i.e. ‘0’ to ‘9’
spaces
newlines
other characters.
entered on standard input or redirected to standard input from a file. Submit the .c file you write to do this via the submission box below.


Hint 1: You should design this program before coding it.

Hint 2: Remember in C, all characters are represented by small integers in the range 0 to 255.

Hint 3: when you compile you program if you create and executable called ‘ex_e’, you will be able to test the program using the following:

ex_e < testdata.txt

where testdata.txt is a file with your test data in it. Try and think about the sort of test data you might need to test your program.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading data file in C

 
0
  #9
Apr 14th, 2008
Originally Posted by tah View Post
HI Sir
I wish to write a program that can do the following
I can assure you that no one over here will prevent you from doing so.

It is customary to read the rules of the forum if you are posting for the first time.
Allow me to invite you to do that now. Click here
For further help read this and this
And one more thing. It is alright to make your own new thread, next time you want to ask a question.
Welcome!!
Last edited by Aia; Apr 14th, 2008 at 5:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Reading data file in C

 
0
  #10
Apr 14th, 2008
I can assure you that no one over here will prevent you from doing so.
i might.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC