473,547 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation Fault using fprintf (really weird)

8 New Member
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format...

basically DIMACS format is:

Expand|Select|Wrap|Line Numbers
  1. c  comment
  2.  
  3. p type nodes edges    //type is alwats edge on my problems, nodes is the number of nodes and edges number of edges
  4.  
  5. e v1 v2   //this means an edge connecting v1 and v2, both integers
  6.  
this is the file im using:

Expand|Select|Wrap|Line Numbers
  1. c FILE: miles500.col
  2. c Translated from Stanford GraphBase File: miles500.gb
  3. c Stanford GraphBase ID: miles(128,0,0,0,500,127,0)
  4. p edge 128 2340
  5. e 1 120
  6. e 1 110
  7. ..... (many many many edges here)
  8. e 128 8
  9. e 128 7
  10.  
the output i want is:

Expand|Select|Wrap|Line Numbers
  1. nodes edges
  2. v1 v2
  3. ...
  4.  
the code is this one:
in was opened with "r"
out was opened with "w+"
both were checked if null

Expand|Select|Wrap|Line Numbers
  1. char line[400];
  2.  
  3.     char* token;
  4.     while( !feof(in))
  5.     {
  6.         fgets(line, 399, in);
  7.         if(line[0] == 'p')
  8.         {
  9.             int nodes, edges;
  10.             strtok(line, " ");
  11.             token = strtok(NULL, " ");
  12.  
  13.             token = strtok(NULL, " ");
  14.             nodes = atoi(token);
  15.  
  16.             token = strtok(NULL, " ");
  17.             edges = atoi(token);
  18.             fprintf(out, "%d %d\n", nodes, edges);
  19.         }
  20.         else if(line[0] == 'e')
  21.         {
  22.             int v1, v2;
  23.             strtok(line, " ");
  24.  
  25.             token = strtok(NULL, " ");
  26.             v1 = atoi(token);
  27.  
  28.             token = strtok(NULL, " ");
  29.             v2 = atoi(token);
  30.  
  31.             fprintf(out, "%d %d\n", v1, v2);
  32.         }
  33.     }

ant it works well..

but for some reason, after writing a lot of edges it sends me a segmentation fault..

original file is:
http://mat.gsia.cmu.edu/COLOR/instances/miles500.col

in the output file i have:
Expand|Select|Wrap|Line Numbers
  1. 128 2340
  2. 1 120
  3. 1 110
  4. 1 109
  5. ... keeps writing ok
  6. 111 84
  7. 111 75
  8.  
thats not all of the original files edges, its like more than a half..

then SEGMENTATION FAULT.. and aborts

what could be happening? some kind of writing limit?
Nov 11 '09 #1
4 8065
jfwfmt
11 New Member
I believe you are probably reading a line from your input file that is missing a token.

Try checking the token value for NULL before passing it to atoi(), then printing a error message and exit. If this shows up a problem, I'd then count input lines as I read them in and print the line number as part of the error message, this will make it easier to find the bad line in the input file.

Checking for NULL is good defensive programming even if "it can't happen".

Good Luck

/s/ Jim WIlliams
Nov 11 '09 #2
RRick
463 Recognized Expert Contributor
jfwfmt is right, you are reading a blank line from the end of the file and this is causing your program to blow up.


fgets stops when it runs into "either" an eof or a <cr>. In your case, fgets stops just before the eof when it reads the last line. The next time around, feof says there is still something in the file, but fgets reads nothing into line. Also, the line buffer has some left over info from the previous strtoks. This gets you to the "e" condition, but blows up on atoi.

Your output file is incomplete, because data is buffered for file output. When your program blows up, the file data in the buffer is thrown away. Put a printf in the "e" condition code and you'll see that you are reading to the end of the input file.


You have a couple of choices to fix the problem. Since fgets returns the number of bytes read, the simplest solution is to check that fgets actually reads something. If not, then exit the loop.
Nov 12 '09 #3
spiralfire
8 New Member
@RRick
That's a really probable issue.. but the thing is i just finished my code, and it some files work, others don't. They are exactly the same in the end of file, no blank line, just the last edge and nothing else, i just can't figure out why this happen.

edit: forgot to say something.. i left the idea of reading an DIMACS file and writing in a simpler format, i now just read the input from the DIMACS file, and it stills send me an error. I tried checking for null tokens, and in the 2 last lines of the file the second token (not the "e", but the first int) gets null and I don't call the atoi.. segmentation fault was solved, the problem now is in the package we are using (GLPK), that just aborts it's execution with no error message, and that just sucks.

Thanks for the replies.
Nov 12 '09 #4
RRick
463 Recognized Expert Contributor
Watch out, there could be a subtle difference between the files that work and the files that don't work.

The original crash and burn problem was caused by the last character in the file being a <cr> (i.e. a carriage return).

If there is no <cr> after the last entry in the file, then the current code will work just fine.
Nov 13 '09 #5

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

Similar topics

2
1923
by: Ron Kneusel | last post by:
I've installed Python 2.3.4 on Red Hat 9 and everything seems to be working fine. Tkinter is available and I can run GUI scripts. However, when I try to start Idle I get a segmentation fault. Using GDB and loading the idle script by hand: >gdb python2.3 (gdb) run .... >>> from idlelib.PyShell import main
6
4756
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation fault. If I just call realloc( ) once before calling malloc( ), it is OK. Why? I am trying to read some double-typed items from infile and save them
0
1718
by: Jonathan Villa | last post by:
I'm trying to add some functionality to an already existing application...this applications works fine, and uses the PHP function pg_pconnect to make a persistent connection to the database. This works very well. My part is to make a new non-persistent connection (or whatever), to a newly created database on the same server. I simply do
1
5358
by: cesco | last post by:
Hi, I'm using the boost library program_options for parsing the command line given to my program as described in the class Parameter: // Main.cpp #include "Parameter.h" using namespace std; int main(int ac, char* av)
11
7691
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a "segmentation fault (core dumped)" error: #include <stdio.h> main() {
0
1915
by: jgarber | last post by:
Hello, I just upgraded MySQLdb to the 1.2.0 version provided by Redhat Enterprise Linux ES4. At that point I began to get segfaults when importing twisted after MySQLdb, but not before. -- RedHat Enterprise Linux ES 4 (fully updated) Python 2.3.4 mysql-python (MySQLdb) version 1.2.0
5
3860
by: lancer6238 | last post by:
Dear all, I'm trying to implement the Smith-Waterman algorithm for DNA sequence alignment. I'm aligning a 2040 bp query sequence against a 5040 bp sequence. I'll be trying to implement a parallel version of it later, that's why I have the MPI timer function to measure the speedup. seq and foundseq0 are the query sequence. size is the length...
3
5948
by: meepror | last post by:
Hi, I bet you've all been asked this a million times but here goes! :) I'm coding an English language dictionary which is stored in a tree by way of the following structure: #define MAXWORDSIZE 100 typedef char word; typedef struct dictionary *dict;
25
3352
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am...
0
7510
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7703
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7463
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
748
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.