Hi
My case is i get the error on runtime while getting data from a file.
i get this error: -
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: -
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?
9 3429
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?
or could i be greater than 3? -
printf("%s : %s\n", title[i++], word);
-
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.
just noticed i is not initialised - it is a local variable so its value will be undefined.
you need to
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. -
strncpy(word, line, n);
-
word[n]='\0';
-
think this is what you need -
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?
any reason why you don't use sscanf() to extract the data from your input, e.g. -
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
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.
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 -
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);
-
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.
it works with feof using while loop.
thanks for the info, horace1
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |