| | |
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
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
>>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
Is that the question -- how to extract each of the numbers in the string ? extract them using sscanf(). There is a simple example
C Syntax (Toggle Plain Text)
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); }
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.
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()"
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.
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.
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;
} >>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.
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.
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,
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,
C Syntax (Toggle Plain Text)
while (input[i]!=EOF) { sscanf(input[i],"%f",&num[i]); printf("My Numeric Array%f",num[i]); i++; }
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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!!
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.
![]() |
Other Threads in the C Forum
- Previous Thread: Shared Memory in C
- Next Thread: browser development in c
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






