File Input Stream Not Loading

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char** argv) 
{
    //first input text stream from file
    //tokenize
    //parse each token against structure functions for each command type
    //structure functions will depend on some symbol in lookup table
    //store data in parse tree nodes
    //connect nodes into an n-tree
    cout << "Open what file?";
    char* str;
    cin >> str;
    fstream myfile(str,ios::in);
    string x;
    if(myfile.is_open())
    {
        while(getline(myfile,x))
        {
            cout << "1";
        }
    }
    cout << x;
    myfile.close();
    
    return 0;
}


I'm trying to load a file input stream to load all the character content into a variable (say from a .java file for example), but for some reason whenever I type in the name of the file I want to stream I get the report: RUN FAILED (exit value 1, total time 2s)

can anybody see anything wrong with my code?
can anybody see anything wrong with my code?
1
2
char* str; //declaring a pointer pointing to some random memory.
cin >> str; //Writing into some random memore. Leads to crash 


Use string:
1
2
3
std::string str;
std::cin >> str;
std::ifstream myfile(str);
wow...even in C pointers were never my friends
got another error now - don't even understand what this one means

no matching function call to 'stdbasic_ifstream<char>::basic_ifstream(std::string&)'

could that be because it doesn't want to accept a string in place of a character pointer?
You can't use a std::string as a filename, so instead convert it to a char using c_str() function in the header file string....
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char** argv) 
{
    //first input text stream from file
    //tokenize
    //parse each token against structure functions for each command type
    //structure functions will depend on some symbol in lookup table
    //store data in parse tree nodes
    //connect nodes into an n-tree
    cout << "Open what file?";
    std::string str;
    cin >> str;
    fstream myfile(str.c_str(),ios::in);
    string x;
    if(myfile.is_open())
    {
        while(getline(myfile,x))
        {
            cout << "1";
        }
    }
    cout << x;
    myfile.close();
    
    return 0;
}

Notice how we use str.c_str() to convert the std::string to a C-String or char array...
You can do that, but I suggest you to turn on C++11 support in your compiler, or upgrade if it is outdated. Aside from saner interface for many funnctions you will get a load of awesome features.
is there any way I can change my getline() function to store the whole document in the variable rather than just 1 line?
is there any way I can change my getline() function to store the whole document in the variable rather than just 1 line?


The important part to remember is that getline retrieves the line and throws away the newline (or whatever delimiter) character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>
#include <string>

int main(){
    using namespace std;

   ofstream src("my_file.txt");
   if(!src.is_open())
      return -1;
   string file, line_holder;
   while(getline(src, line_holder)){
      file += line_holder + "\n"; //Collect each line, re-adding the thrown away character
      line_holder.clear();
   }
   //file.pop_back(); //Optional to get rid of the extra newline character we inserted
   src.close();

   cout << "I read:\n\n" << file;

   return 0;
}
Last edited on
Topic archived. No new replies allowed.