Find the answer to your Linux question:
Results 1 to 3 of 3
Hi Experts 1)I was reading the file into a buffer and I used the following code as follows:- ifstream inputFile("filePath"); if ( inputFile.is_open() ) { char* data = new char[fileSize]; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    9

    problem in linux with respect to file stream reading


    Hi Experts

    1)I was reading the file into a buffer and I used the following code as follows:-

    ifstream inputFile("filePath");

    if ( inputFile.is_open() )
    {

    char* data = new char[fileSize]; // create buffer to read data from the file

    inputFile.read( data, fileSize );
    }


    I am trying to call above code once for file A and second time for file B. For file A its working fine, for File B ; it gives segmentation fault as soon as control hit the read function for file B.The problem was coming in release mode, its working fine in debug mode. To get rid of this read function's failure I replaced these above stream function with Fread, Fopen C file reading function and I am able to read the file in to buffer for both file A and File B.

    2) Once I am able to solve problem by using fopen, Fread etc functions, then I am facing problem in following code:-

    string s;
    stringstream str( string( data , fileSize ) );
    while( getline( str, s ))
    {
    // perform some opertaion on s
    }

    The getline function was failing for file B , its working fine for File A (where all the file data exist in the buffer data, which I checked by printing the data), again the problem coming in release mode , debug mode is working fine.

    Thanks & Regards
    Vicky

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,566
    Quote Originally Posted by vickyCool View Post
    Hi Experts

    1)I was reading the file into a buffer and I used the following code as follows:-

    ifstream inputFile("filePath");

    if ( inputFile.is_open() )
    {

    char* data = new char[fileSize]; // create buffer to read data from the file

    inputFile.read( data, fileSize );
    }


    I am trying to call above code once for file A and second time for file B. For file A its working fine, for File B ; it gives segmentation fault as soon as control hit the read function for file B.The problem was coming in release mode, its working fine in debug mode. To get rid of this read function's failure I replaced these above stream function with Fread, Fopen C file reading function and I am able to read the file in to buffer for both file A and File B.

    2) Once I am able to solve problem by using fopen, Fread etc functions, then I am facing problem in following code:-

    string s;
    stringstream str( string( data , fileSize ) );
    while( getline( str, s ))
    {
    // perform some opertaion on s
    }

    The getline function was failing for file B , its working fine for File A (where all the file data exist in the buffer data, which I checked by printing the data), again the problem coming in release mode , debug mode is working fine.

    Thanks & Regards
    Vicky
    Please provide all the relevant code, and place it inside code blocks so it can be more easily read.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jan 2012
    Posts
    9
    Quote Originally Posted by Rubberman View Post
    Please provide all the relevant code, and place it inside code blocks so it can be more easily read.
    I am able to foud problem in my code. It was due to some window function failure which caused rest of the thing to go wrong...


    Thanks & Regards
    Vikas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •