473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Error while getting data from a file

254 100+
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
9 3429
horace1
1,510 Expert 1GB
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
nickyeng
254 100+
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
horace1
1,510 Expert 1GB
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
horace1
1,510 Expert 1GB
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
horace1
1,510 Expert 1GB
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
nickyeng
254 100+
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
horace1
1,510 Expert 1GB
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
nickyeng
254 100+
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
nickyeng
254 100+
it works with feof using while loop.

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

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

Similar topics

1
by: ketulp_baroda | last post by:
Hi I am storing the attachment in the database. For some file the data gets stored in the database but for other files it gives a MySql error. For eg. when I am trying to attach the file say...
3
by: ketulp_baroda | last post by:
Hi I am storing the attachment in the database. For some file the data gets stored in the database but for other files it gives a MySql error. For eg. when I am trying to attach the file foo.txtit...
4
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column...
6
by: genc ymeri | last post by:
Hi, We are struggeling to upload a file through a C# webClient into JBoss web server. Meanwhile we are able to upload a file from the webserver itself. The problem is only with C# webClient . The...
25
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from...
7
by: Martin Strojek | last post by:
Hi, I have the following problem with developing some web site. I use Visual Studio 2003 to build a website. I tried Windows 2003 Server and switched now back to Windows XP with PWS but the...
4
by: jf li | last post by:
I have a Asp.net web application and a Asp.net Web service application. The Web application is using HtmlInputFile to get a 50M size of file selected by end user, read the data of this file and...
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
1
by: Alexander Higgins | last post by:
>>Thanks for the response.... Point Taken but this is not the case. Thus, if a person writes a text file on her or his computer and does not use UNICODE to save it, the current code page is...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.