By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Error while getting data from a file

100+
P: 254
Hi

My case is i get the error on runtime while getting data from a file.
i get this error:
Expand|Select|Wrap|Line Numbers
  1. Record of 12344 is found!
  2.        5 [main] testing 4272 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack) 
  3. Segmentation fault (core dumped)
  4.  
I use cygwin gcc package to compiled it.
here is the code that i thought it might the error area:
Expand|Select|Wrap|Line Numbers
  1. char line[100];
  2. char *title[] = {"Student ID", "Student Name", "Subject Code", "Mark"}
  3. printf("\nRecord of %s is found!\n", id);
  4.  
  5. while(fgets(line, 100, ptr) != NULL) {   // read line by line
  6.  
  7.     char word[20];
  8.  
  9.         int n;
  10.     int i;
  11.  
  12.     for(n = 0; n < strlen(line); n++)
  13.     {
  14.         if( line[n] == ' ' )   // See if it is a space
  15.         {
  16.             strncpy(word, line, n);   
  17.             line = strstr(line, " ");     // get the whole line from first occurrances of " ".
  18.             printf("%s : %s\n", title[i++], word);
  19.         }
  20.     }
  21.     line = "";    // set the line to blank
  22.     break;
  23. }
  24.  
anyone helps?
Mar 7 '07 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 1,510
try printing the records as you read them with fgets() - you will then be able to see which record caused the segmentation error. I would assume you have exceeded an array bounds. For example, could this copy > 20 characters?
Expand|Select|Wrap|Line Numbers
  1.             strncpy(word, line, n);   
  2.  
or could i be greater than 3?
Expand|Select|Wrap|Line Numbers
  1.             printf("%s : %s\n", title[i++], word);
  2.  
Mar 7 '07 #2

100+
P: 254
the data from file is the format like this:
------------------------------------
data1 data2 data3 data4
......
------------------------------------

each data wont exceed > 20 characters.

so this problem is not an issue.

Next,
the i variable wont be incremented unless the if block runs, right? so i have no idea what else get wrong..

thanks for the info.
Mar 7 '07 #3

Expert 100+
P: 1,510
just noticed i is not initialised - it is a local variable so its value will be undefined.
you need to
Expand|Select|Wrap|Line Numbers
  1.     int i=0;  // ** set i = 0
  2.  
strdncpy() does not null terminate a string if the length of the string in line is > n, you need to make sure it is null terminated, e.g.
Expand|Select|Wrap|Line Numbers
  1.             strncpy(word, line, n);  
  2.             word[n]='\0'; 
  3.  
Mar 7 '07 #4

Expert 100+
P: 1,510
think this is what you need
Expand|Select|Wrap|Line Numbers
  1.     int n;
  2.     int i=0;  // ** set i = 0
  3.  
  4.     for(n = 0; n < strlen(line); n++)
  5.     {
  6.         if( line[n] == ' ' )   // See if it is a space
  7.         {
  8.         strncpy(word, line, n);  
  9.                 word[n]='\0';                  // null terminate
  10.             line = strstr(line+1, " ");     // get the whole line from first occurrances of " ".
  11.         printf("%s : %s\n", title[i++], word);
  12.         n=0;                             // reset to start of new string
  13.         }
  14.     }
  15.  
what if the Student name has a space in it?
Mar 7 '07 #5

Expert 100+
P: 1,510
any reason why you don't use sscanf() to extract the data from your input, e.g.
Expand|Select|Wrap|Line Numbers
  1.     char *title[] = {"Student ID", "Student Name", "Subject Code", "Mark"};
  2.     char data[100]="data1 data2 data3 data4 ";
  3.     char id[10], name[10], code[10], mark[10];
  4.     sscanf(data, "%s%s%s%s", id, name, code, mark);
  5.     printf( "%s %s\n %s %s\n %s %s\n %s %s\n",
  6.          title[0], id, title[1], name, title[2], code, title[3], mark);
  7.  
when run gives
Student ID data1
Student Name data2
Subject Code data3
Mark data4
Mar 7 '07 #6

100+
P: 254
the reason is i must save data into a file, and any function in the program need data from a file, then i must retrieve data from file.
Mar 7 '07 #7

Expert 100+
P: 1,510
the reason is i must save data into a file, and any function in the program need data from a file, then i must retrieve data from file.
what about fscanf()
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html

e.g. reading one recond
Expand|Select|Wrap|Line Numbers
  1.   FILE * pFile;
  2.   char *title[] = {"Student ID", "Student Name", "Subject Code", "Mark"};
  3.   char id[10], name[10], code[10], mark[10];
  4.   pFile = fopen ("myfile.txt","r");
  5.   if(pFile == NULL)
  6.     { printf("file open failed!"); system("pause"); exit(1); }
  7.   fscanf(pFile, "%s%s%s%s", id, name, code, mark);
  8.   printf( " %s %s\n %s %s\n %s %s\n %s %s\n",
  9.          title[0], id, title[1], name, title[2], code, title[3], mark);
  10.   fclose (pFile);
  11.  
Mar 7 '07 #8

100+
P: 254
what if i have these data in a file:
-------------------------------------
data1 data2 data3 data4
dat1 dat2 dat3 dat4
......
...... // keep continue to have data
-------------------------------------

your fscanf is works with one line data i think.
Mar 8 '07 #9

100+
P: 254
it works with feof using while loop.

thanks for the info, horace1
Mar 8 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.