Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Segment default error file reading c++

Posted on 2005-02-24
6
Medium Priority
?
220 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

580 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