Error while getting data from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    Error while getting data from a file

    Hi

    My case is i get the error on runtime while getting data from a file.
    i get this error:
    Code:
    Record of 12344 is found!
           5 [main] testing 4272 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack) 
    Segmentation fault (core dumped)
    I use cygwin gcc package to compiled it.
    here is the code that i thought it might the error area:
    Code:
    char line[100];
    char *title[] = {"Student ID", "Student Name", "Subject Code", "Mark"}
    printf("\nRecord of %s is found!\n", id);
    
    while(fgets(line, 100, ptr) != NULL) {   // read line by line
    
    	char word[20];
    
            int n;
    	int i;
    
    	for(n = 0; n < strlen(line); n++)
    	{
    		if( line[n] == ' ' )   // See if it is a space
    		{
    			strncpy(word, line, n);   
    			line = strstr(line, " ");     // get the whole line from first occurrances of " ".
    			printf("%s : %s\n", title[i++], word);
    		}
    	}
    	line = "";    // set the line to blank
    	break;
    }
    anyone helps?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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?
    Code:
    			strncpy(word, line, n);
    or could i be greater than 3?
    Code:
    			printf("%s : %s\n", title[i++], word);

    Comment

    • nickyeng
      Contributor
      • Nov 2006
      • 252

      #3
      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.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        just noticed i is not initialised - it is a local variable so its value will be undefined.
        you need to
        Code:
        	int i=0;  // ** set i = 0
        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.
        Code:
                    strncpy(word, line, n);  
                    word[n]='\0';

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          think this is what you need
          Code:
              int n;
          	int i=0;  // ** set i = 0
          
          	for(n = 0; n < strlen(line); n++)
          	{
          		if( line[n] == ' ' )   // See if it is a space
          		{
          		strncpy(word, line, n);  
                          word[n]='\0';                  // null terminate
          	        line = strstr(line+1, " ");     // get the whole line from first occurrances of " ".
          		printf("%s : %s\n", title[i++], word);
          		n=0;                             // reset to start of new string
          		}
          	}
          what if the Student name has a space in it?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

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

            Comment

            • nickyeng
              Contributor
              • Nov 2006
              • 252

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

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by nickyeng
                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.c om/reference/clibrary/cstdio/fscanf.html

                e.g. reading one recond
                Code:
                  FILE * pFile;
                  char *title[] = {"Student ID", "Student Name", "Subject Code", "Mark"};
                  char id[10], name[10], code[10], mark[10];
                  pFile = fopen ("myfile.txt","r");
                  if(pFile == NULL)
                    { printf("file open failed!"); system("pause"); exit(1); }
                  fscanf(pFile, "%s%s%s%s", id, name, code, mark);
                  printf( " %s %s\n %s %s\n %s %s\n %s %s\n",
                         title[0], id, title[1], name, title[2], code, title[3], mark);
                  fclose (pFile);

                Comment

                • nickyeng
                  Contributor
                  • Nov 2006
                  • 252

                  #9
                  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.

                  Comment

                  • nickyeng
                    Contributor
                    • Nov 2006
                    • 252

                    #10
                    it works with feof using while loop.

                    thanks for the info, horace1

                    Comment

                    Working...