Hi, I have yet another problem after spending a full day today working on my program. What I am trying to do is to read a textfile, and put the values within the textfile into proper categories within the struct. So in this case I have built a struct containing the members, which are categories that I want the values to be in.
Here is my struct:
Code:struct database_input { int categoryA; int categoryB; double categoryC; int categoryD; };
where the format of one line of the data
(the textfile consists of alot of lines of the same format):
in which I want the first number (1.3.1.2) to go into the first category and the second number (3.97438e-05) to go in the 2nd category and so on.1.3.1.2 = 3.97438e-05 121 1
What I did to get 1.3.1.2 to be and int is to establish a loop to skip the periods and only read 1312 as a string, then typecast it back to int. For the second number, since everything is in e-05, I only read the number up to 3.97438, and then I typecast to double and divide by 10000.
Here is how I call the function in main:
the function code is:Code:int main() { struct database_input data[99999]; initializing_structs(data); //for setting all values to 0 read_input( inputname , data); ...
I know this is a long post, but please keep on reading...Code:void read_input( FILE *ifp, struct database_input *data) { int i=0; char number = NULL; char *categoryA_number = NULL; char *categoryC_f = NULL; double categoryC_number; char *categoryB_number; while( i<99999 && feof(ifp) != 1) { while( number != ' ') { fscanf( ifp, number); if (number != '.') strcat(categoryA_number, number); } data[i].categoryA = (int)*categoryA_number; ifp = ifp+2; number = NULL; while( number != 'e') { fscanf( ifp,number); strcat(categoryC_f, number); } categoryC_number = (double)*categoryC_f; data[i].categoryC = categoryC_number/10000; ifp = ifp+5; number = NULL; while( number != ' ') { fscanf( ifp, number); if (number != '.') strcat(categoryB_number, number); } data[i].categoryB = (int)*categoryB_number; ifp = ifp+1; number = NULL; fscanf( ifp, number); data[i].categoryD = (int)number; number = NULL; while( number != '\n') { fscanf( ifp, number); ifp++; } i++; } }
The program when compiled, had many warnings, like:
When I run the program, it just went into segmentation error. I already tried to simplify the function as much as possible, but it still ends up with a long code. Can somebody please help me to find what was wrong with this code, and if possible, ways to fix it? Since the code that I have wrote is a bit confusing, don't hold back in asking me questions about how the logic of the code works.projectmain.c:91: warning: conflicting types for ‘initializing_structs’
projectmain.c:54: warning: previous implicit declaration of ‘initializing_structs’ was here
projectmain.c:107: warning: conflicting types for ‘read_input’
projectmain.c:55: warning: previous implicit declaration of ‘read_input’ was here
projectmain.c: In function ‘read_input’:
projectmain.c:109: warning: initialization makes integer from pointer without a cast
projectmain.c:121: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
projectmain.c:123: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
projectmain.c:128: warning: assignment makes integer from pointer without a cast
projectmain.c:132: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
projectmain.c:133: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
projectmain.c:139: warning: assignment makes integer from pointer without a cast
projectmain.c:143: warning: passing argument 2 of ‘fscanf’ makes pointer from integer without a cast
projectmain.c:145: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
projectmain.c:152: warning: assignment makes integer from pointer without a cast
as for not feof(), I am having difficulties in inplementing the fix in the faq into my code since I am not reading the whole line, I am reading from character to character.
Many Thanks.