#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1

    C++ vector lineparser


    Hello
    I'm trying to code some C++ program that makes a vector open a file, and print each line with its number.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main(int argc, char * argv[])
    {
        ifstream in("linetest.txt");
        vector<string> v;
        string line;
        while(getline(in, line))
        {
            v.push_back(line);
        }
        for(int i = 0; i < v.size(); i++)
        {
            cout << i << " " <<  v[i] << endl;
        }
    in.close();
        return 0;
    }
    But its just not printing anything. By the way the .txt is in the same directory and I made a similar program with vectors made of floats but that doesn't open files.
    Some insight? Thanks

    EDIT: Now made it a project on code::blocks and put the .Txt in the .Exe repertory, and avg says its a virus...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You should be checking that the file opens correctly and take action if it doesn't.
    Now made it a project on code::blocks and put the .Txt in the .Exe repertory, and avg says its a virus...
    This is probably being caused because of where you are placing your projects.

    What directory are you using for your projects?

    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1
    Am I forced to check if the file exist ? Because I saw the solution of my book "Thinking in C++" and it's exactly the same except no .close();
    My repertory is in my documents.
    So should I use assert or some other thing ? I haven't seen how to check errors yet :/
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Am I forced to check if the file exist ?
    It's considered a good practice to always check the return values of standard functions. These return values usually will tell you why things aren't working properly.

    I haven't seen how to check errors yet
    The easiest way to check for stream errors is with an if statement:
    Code:
       ifstream in("linetest.txt");
       // Check the stream state to insure the file opened.
       if(!in)
       {
          // The file failed to open properly. Tell the user, then take some appropriate action.
       }
    it's exactly the same except no .close();
    Do you know that you don't need to explicitly close the stream in C++? The class destructor will properly close the file when the file goes out of scope.


    Jim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    31
    Rep Power
    1
    Thanks a lot Jim !
    I added the file checking "if(!in)" and I put "cout << "Failed to open file" << endl;" inside, and now it always tell me I failed to open file in the console output !. I don't really understand, its the exact same name of the file, in the same repertory Release of my project of code::blocks, so .exe and .txt are in same directory and its still not working... Maybe I need to add the full path of the file (I tried) ? Or to put \\(or some other character) before linetest.txt ?
    Thanks in advance

    EDIT: realised we need to have a char * but its still not working. Please don't answer here but on my new topic "class that read file and put them in a string" !

IMN logo majestic logo threadwatch logo seochat tools logo