Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Newbie: Passing an fstream to a function to read data

I have a text file named num.txt who's only contents is the line 123. Then I have the following:

void alt_reader(ifstream &file, char* line){
    file.read(line, 3);
    cout << "First Time: " << line << endl;
}

int main() {
    ifstream inFile;
    int num;
    inFile.open("num.txt");
    alt_reader(inFile, (char*)&num);
    cout << "Second Time: " << num << endl;
}

The output is:

First Time: 123
Second Time: 3355185

Can you help me figure out how to get an fstream that is read in a function still assign the variable in main? I'm doing this because alt_reader really has a lot more to it, but this is the part I'm stuck on. Thanks a lot for the help.

UPDATE: Using Bill Oneal's comments, I've written

void alt_reader(ifstream &file, stringstream &str, int n){
    char buffer[n+1];
    file.read(buffer, n);
    buffer[n] = 0;
    str << buffer;
    cout << "First Time: " << buffer << endl; //First Time: 123
}

int main() {
    ifstream inFile;
    stringstream strm;
    int num;
    inFile.open("num.txt");
    alt_reader(inFile, strm, 3);
    cout << "Second Time: " << num << endl; //Second Time: 123
}

Thanks. Any critiques with what's there now?

like image 529
physicsmichael Avatar asked Apr 05 '10 22:04

physicsmichael


2 Answers

You have at least two problems here.

In main():

  1. You should be passing a string buffer into alt_reader, instead you're passing an int.
  2. You seem to want to read the string '123' but want an int to have the value 123.

You could do:

void alt_reader(ifstream &file, char* line){
    file.read(line, 3);
    line[3]=0;
    cout << "First Time: " << line << endl;
}

int main() {
    ifstream inFile;
    inFile.open("num.txt");

    char buffer[128];
    alt_reader(inFile, buffer);

    int num=atoi(buffer);
    cout << "Second Time: " << num << endl;

    return 0;
}

Note that I've added line[3]=0; to alt_reader and atoi does the conversion from string (a scii) to int.

like image 32
quamrana Avatar answered Sep 19 '22 01:09

quamrana


The first time you printed the variable, you printed it as a char *, printing treating the file as a text file (And you're lucky you didn't crash). The second time you printed it, you reinterpreted it as an int, making the representation completely different.

Whenever you cast pointers from one type to another type you are usually invoking undefined behavior. Since char has no standard relation to int, you have it here.

EDIT: To answer your comment question:

#include <sstream>

void foo(std::stream &str) {
 str << "42\n";
};

int main() {
 int aNumber;
 std::stringstream aStringStream;
 foo(aStringStream); //Pass our stream to the function. It contains
    //"42\n" when the function returns.
 aStringStream >> aNumber; //aNumber == 42
 aNumber += 10; //aNumber == 52;
 std::cout << aNumber; //Print "52"
}
like image 64
Billy ONeal Avatar answered Sep 18 '22 01:09

Billy ONeal