954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading data file in C

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

Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

int main()
{
    char line[] = "123 234.56 789";
    int a, b;
    float c;
    sscanf(line,"%d %f %d", &a, &c, &b);
    printf("%d %f %d", a, c, b);
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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()"

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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;
}
Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

>>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++)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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,

while (input[i]!=EOF)
   {
         sscanf(input[i],"%f",&num[i]);
         printf("My Numeric Array%f",num[i]); 
         
         i++;
   }
Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

tah
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
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!! :)

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
I can assure you that no one over here will prevent you from doing so.

i might.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
i might.


Oh, yeah? I would like you to try, stupendous man ;)

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

silly mortal, you doubt my powers?

watch me battle my way through pedantic digressions about the dangers of pseudo-randomness and the evils of scanf, while flying by the seat of my pants deep into the lair of non-error-checked command line input. all in the name of clarity i say!

whoops... here comes Naure

*ahem* now if you'll excuse me, i believe i have other um... threads to rescue.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Ok,
As you said I defined i variable and declare another variable called cnt. The piece of code that I changed is below. But if I use cnt=0 the loop never entered. And when I changed cnt to 1 the result are same as my previous while loop.
I can't find the error please notice this,

for(i=0,cnt=0;i<cnt;i++,cnt++)
   {     
         sscanf(input[i],"%f",&num[cnt]);
         printf("My Numeric Array%f",num[cnt]);  
   }
Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

for(i=0,cnt=0;i<cnt;i++,cnt++)
Well, if i = 0 and cnt = 0 this i < cnt will never allow the loop to execute, because i is never smaller than cnt. Don't you think?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

I know the loop don't enter. if you see i mentioned that i my question. My question is what i have to do to get the numbers from my text array.

Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
I know the loop don't enter. if you see i mentioned that i my question.

Yes, he saw that. He also explained your problem for you, so that you can come up with a solution yourself.
This is a great attitude if you want people to ignore your problem, but I'm in a fairly good mood, so: for(int i=0; i< (how many elements are in your array); i++)

Now remove all the cnt's because they don't make any sense.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
Yes, he saw that. He also explained your problem for you, so that you can come up with a solution yourself. .

he :icon_eek: Aia's avatar looks female to me :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
he :icon_eek: Aia's avatar looks female to me :)


Whoops. I have avatars switched of, so I didn't notice it... :icon_redface:

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

I used this loop before this someone said don't use that define as such, please read the main program (I want to read double data from string array that made from file). When I use these loops I can't access the string array anyway. And when I try to print I see 0.0000.
If I want to describe the case in my input [] array now I have the string of numbers in double format I want to have access to each numbers in second array (i.e. num[]).
Please

for(i=0;i<150;i++)
   {     
         sscanf(input[i],"%f",num[i]);
         printf("My Numeric Array%f",num[i]); 
         /*This only print the string and 0.00000 for all of my num[]*/  
   }
Afi83
Light Poster
37 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 
he :icon_eek: Aia's avatar looks female to me :)
Whoops. I have avatars switched of, so I didn't notice it... :icon_redface:

Vanity, oh, vanity.
Everything is vanity.
What today is,
Tomorrow does not exist.
Must appearance still persist?
Forgive me for the indulgence of putting the thought into rhyme.


I'm not quite sure of what you want. Perhaps something like this?

for ( i = 0; i < 150; i++ )
{     
         if ( sscanf( input[i], "%f",  &num[i] ) == 1 )
             printf( "My Numeric Array[%d] = %f\n", i, num[i] );
         else
             break; 
         
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You