segmentation problem with file


 
Thread Tools Search this Thread
Top Forums Programming segmentation problem with file
# 1  
Old 12-10-2010
segmentation problem with file

Hey guys I am reading a file in c that has a record like this:

1002:credit card:c:500.000000
2002:cred:d:200.000000

I am trying to read the file like this but I am getting a segmentation fault. Any ideas why?

Code:
while (fscanf(fp1, "%d:%[^:]:%c:%lf\n",&accountnumb[i],mainname[i],&type,&transactionc[i]) != EOF)
  {
  
  if(type=='d')
  {
  transactiond[i]=transactionc[i];
  }
  
      else
    {
    transactionc[i]=transactionc[i];
    }

# 2  
Old 12-11-2010
Could we have a larger snippet of code that includes the definitions of your variables please.
# 3  
Old 12-12-2010
Quote:
Originally Posted by citaylor
Could we have a larger snippet of code that includes the definitions of your variables please.

Here are the variable definitions.

Code:
int i=0,k=0,counter=0,l=0,account,j=0;
char type,choice,name[NUMBS+1][NUMBS+1],mainname[NUMBS+1][NUMBS+1];
int accountnumb[ACCOUNTS];
double transactionc[NUMBS];
double transactiond[NUMBS];
FILE *fp, *fp1;

# 4  
Old 12-12-2010
What is NUMBS? What is fp1? Are you sure fp1 is a valid file pointer and not NULL or uninitialized? Does I ever go beyond NUMBS? On first glance this looks correct, but if the arrays aren't large enough, you could start mangling local variables and stack frames when you exceed them...

Last edited by Corona688; 12-12-2010 at 03:23 AM..
# 5  
Old 12-12-2010
I would say your quickest method of finding your issue is to compile the program with debugging enabled ("-g"), run the program , wait for the core-dump. Then load the program up in a debugger, against the core. For example gdb:
Code:
gdb ./prog ./core

If you type "where" it will tell you the line number it is core-dumping. You can then print out the variables so you can tell what is the problem, for example in your snippet "i" could be less then or greater than "NUMBS" which would make it coredump.

If you havent got access to a debugger I suggest you start using "printf's" to find out where the problem is. Lines such as:
Code:
printf("%d\n",__LINE__);

can show the line where it is core-dumping. Then you can add printf's of the variables around where it core-dumps.

I hope this helps...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Shell Programming and Scripting

Count Segmentation fault and write to the file

Hi everyone Need to get version of npm application that have several output like this: root: nmp -version 10 root: nmp -version 10 root: nmp-new -version 3.1 root: nmp-old -version Segmentation fault count them , after that write to the file like this: 10 2 3.1 1 (1 Reply)
Discussion started by: indeed_1
1 Replies

3. Fedora

Segmentation fault while trying to recover file with extundelete

hello, I accidentally removed a directory with its contents with rm -r and I don't have a backup. I got to know about extundelete utility on linux and downloaded on my Linux Fedora 28 notebook PC. The file system is ext4. I log in to the system as single user mode and unmount the /home directory... (2 Replies)
Discussion started by: milhan
2 Replies

4. Debian

Linux, Debian - Segmentation Fault problem.

Hi guys, first of all apologize for my English... I have a big problem with "Segmentation fault", when running my game server. Console: (gdb) bt full #0 0x0000000000000000 in ?? () No symbol table info available. #1 0x00007ffff702aca4 in std::basic_ostream<char,... (1 Reply)
Discussion started by: Arson.
1 Replies

5. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

6. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

7. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

8. Shell Programming and Scripting

script to Read Error,jbase,voilation,segmentation words from a file

Hi, i need a script to find words "error,jbase,voilation,segmentation" from a file(verify.txt) and if got any of the above words then send an email and if wont find any then execute the cron job. every time it read file verify.txt it must first clear the previous data in the file . Kindly... (3 Replies)
Discussion started by: Mujtaba khan
3 Replies
Login or Register to Ask a Question