?
Solved

Segment default error file reading c++

Posted on 2005-02-24
6
Medium Priority
?
216 Views
Last Modified: 2010-08-05
When I try to read a line from a file I get a Segment default.  Below are the files and some code, they are commented I hope well enough and the point where it crashes is also identified in the comments.  Could some one help me figure this out, I'm lost.  Any suggests are greatly appreciated.
thank you
Steve






filecontents:
--------------------------------
NC_000964.faa
------------------------------
some code where the error occurs
ifstream sequencefile(sequences);

cout<<sequences<<"\n";



char c;

for(;;){


c = sequencefile.peek();
cout<<c<<" this is c\n";

if(c == EOF){break;}

else{

cout<<"p1\n";
sequencefile.getline(temp3,16,'\n'); // this is where the Segmentation fault occurs********************

cout<<"p2\n";



temp3 = strtok(temp3,"\n\t\040");

0
Comment
Question by:sai4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13397840
What type is temp3, and what's it's size?

It would help if you where to post much more of your code, so we can get the complete picture.

I think temp3 is a pointer, with no allocated space.

Make sure it gets space allocated.

Once you use it to allocate space, you should not be using it with strtok, because you'll loose the original pointer to the allocated space.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13397859
Should use a new pointer.

char * NewPointer =  strtok(temp3,"\n\t\040");

Then use NewPointer for the rest of your calls to strtok
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13397870
>>sequencefile.getline(temp3,16,'\n'); // this is where the Segmentation fault occurs

What is 'temp3'? If it is a

char* temp3;

this is most likely to fail unless you use e.g.

temp3 = new char [ 16 + 1];

to assign some memory. If it is a

char temp3[16];

it'll fail also, since you need space for the null terminator.

However, using

char temp3[16 + 1];

should work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 13397880
  char seps[]   = "\n\t\040";
   char * token = strtok( temp3, seps );
   while( token != NULL )
   {
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13399744
most probably some thing wrong with temp3, to give you an example how to read, see the code below, also check if file is opened for reading.

#include <iostream>
#include <fstream>
using namespace std;

int read()
{
     fstream file("c:\\temp\\data.txt");
     char buf[256], c;
     
     while(file.good())
     {
          file.getline(buf, sizeof(buf));
          cout << buf << endl;
          sscanf(buf, "%c", &c);
          cout << c <<endl;
     }
     
     return 0;
}
0
 

Author Comment

by:sai4
ID: 13399978
thanks, using new got rid of the error. I'm guessing not having memory allocated for the pointer caused access violations?
Thanks again
Steve
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question