I am trying to write a program to read a text file. The text file has the following format:
filename section varname1 varname2 varname3 ... varname(n)
There can be multiple lines of this line. The filename, section, and varnames can be of varying length.
Here is an example of the text file that I need to read.
lions.txt sec_2.5-1 height weight color gender
zebras.txt sec_12.11-22 speed height gender coloration markings location
Here is my code:
The problem that I can't seem to solve is that the filename is not getting copied into the variable "ch[i]".Code:#include <stdio.h> #include <string.h> int main() { //********************************** // Open "animals.txt" input file //********************************** // animal data FILE *f; char s[500], a1[20], a2[20], a3[20], a4[20]; char file_name[10][10], ch [10[10]; f=fopen("file_list.txt","r"); if (!f) return 1; while (fgets(s,sizeof(s),f)!=NULL) //while not EOF // scan one line at a time sscanf (s, "%s %s %s %s",a1,a2,a3,a4); strncpy (ch[i], a1, 9); file_name[i][1] = ch[i][1]; printf("file_name = %s\n",file_name[i][1]) ; printf("%s\n",s); fclose(f); return 0; }
Thanks in advance for any help!



LinkBack URL
About LinkBacks


