I wrote a translator, that reads a DIMACS graph format and writes to a simpler format...
basically DIMACS format is: -
c comment
-
-
p type nodes edges //type is alwats edge on my problems, nodes is the number of nodes and edges number of edges
-
-
e v1 v2 //this means an edge connecting v1 and v2, both integers
-
this is the file im using: -
c FILE: miles500.col
-
c Translated from Stanford GraphBase File: miles500.gb
-
c Stanford GraphBase ID: miles(128,0,0,0,500,127,0)
-
p edge 128 2340
-
e 1 120
-
e 1 110
-
..... (many many many edges here)
-
e 128 8
-
e 128 7
-
the output i want is:
the code is this one:
in was opened with "r"
out was opened with "w+"
both were checked if null - char line[400];
-
-
char* token;
-
while( !feof(in))
-
{
-
fgets(line, 399, in);
-
if(line[0] == 'p')
-
{
-
int nodes, edges;
-
strtok(line, " ");
-
token = strtok(NULL, " ");
-
-
token = strtok(NULL, " ");
-
nodes = atoi(token);
-
-
token = strtok(NULL, " ");
-
edges = atoi(token);
-
fprintf(out, "%d %d\n", nodes, edges);
-
}
-
else if(line[0] == 'e')
-
{
-
int v1, v2;
-
strtok(line, " ");
-
-
token = strtok(NULL, " ");
-
v1 = atoi(token);
-
-
token = strtok(NULL, " ");
-
v2 = atoi(token);
-
-
fprintf(out, "%d %d\n", v1, v2);
-
}
-
}
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: -
128 2340
-
1 120
-
1 110
-
1 109
-
... keeps writing ok
-
111 84
-
111 75
-
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?
4 8065
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
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.
@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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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
|
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)
|
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()
{
| |
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
|
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...
|
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;
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |