I am confused with this code that somebody gave me

This is a discussion on I am confused with this code that somebody gave me within the C++ Programming forums, part of the General Programming Boards category; I was given this code from a thread that I created (i cannot remember who) and I would like somebody ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    I am confused with this code that somebody gave me

    I was given this code from a thread that I created (i cannot remember who) and I would like somebody (preferably the same person who gave it to me) to explain what each line does ('cause i've got no idea). And also, when I compile it (in MSVC++ 6.0), it does nothing but sit there!! It is meant to display all of the lines of a text file. I really wanted it to store each line in a variable if it wasn't empty, but anyway.....

    ...here is the code:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>

    using namespace std;

    int main()
    {
    ifstream file("myfile.txt"); // or whatever
    vector<string> Lines;
    string CurrentLine;

    while(!file.eof())
    {
    getline(file, CurrentLine);
    if(!CurrentLine.empty()) // store it if not empty
    Lines.push_back(CurrentLine);
    }

    // show the stored lines
    for(int i = 0; i < Lines.size(); i++)
    cout << Lines[i] << endl;

    file.close();

    return 0;
    }

    Thanks heaps
    -Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Is the file in the same directory as the .exe? I know MSVC likes to put debug executables in the debug folder that contains the workspace, and not in in the same folder as the workspace itself.

    #include <iostream>
    #include <fstream>
    #include <string> // for STL string
    #include <vector> // for STL vector

    using namespace std; // ugly, but gets rid of need to prefix vector/string with std::

    int main()
    {
    ifstream file("myfile.txt"); // open my myfile.txt in same dir as .exe
    vector<string> Lines; // make a vector of string and call it lines
    string CurrentLine; // string used to store the current line in the while loop

    while(!file.eof())
    {
    getline(file, CurrentLine); // input everything on the next line in the file to currentline
    if(!CurrentLine.empty()) // store it if not empty
    Lines.push_back(CurrentLine); // put the current line into the vector
    }

    // show the stored lines
    for(int i = 0; i < Lines.size(); i++) // for all lines
    cout << Lines[i] << endl; // output them

    file.close(); // close the file

    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    thanks for replying, but the program still just sits there and does nothing when I compile it/excecute it in MSVC++ 6.0. And when I do compile it, it gives me the folloing warnings:
    <CODE>
    c:\program files\microsoft visual studio\myprojects\lines\main.cpp(28) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,st d::allocator<
    char> >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
    c:\program files\microsoft visual studio\myprojects\lines\main.cpp(28) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std: :char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,st d::allocator<char>
    >,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > &,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
    c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > >
    >::vector<std::basic_string<char,std::char_traits< char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
    c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_trai ts<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > >
    >::~vector<std::basic_string<char,std::char_traits <char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_ traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
    </CODE>
    And could you also explain to me what a "vector" is or point me in the direction of a good tutorial on it.

    Thanks
    -Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You need to put myfile.txt in the same folder as your workspace and it will work.

    The warnings don't matter, perhaps someone can tell you the correct way to disable them in source (#pragma disable ErrorNum or something like that)
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    yes!! My god you are a legend! It worked! THANKS!!! ...ok, now I only have one question: If I wanted to use the lines that this program displays in another function in the program, how would I read them all into an array...i'll show you what i'm talking about:

    var1 = array[0]; // the first line of the text file is stored in var1
    var2 = array[1]; // the second stored in var2
    var3 = array[2]; // and the third in var3...
    //... and it keeps going

    would I be able to do something like this (after having your code executed at the beginning of the program) for an unknown amount of lines?

    Thanks for replying to my thread
    -Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    A. it's not my code
    B. A vector acts like an array, except it has greater functionality.

    string firstLine = Lines[0];
    string secondLine = Lines[1]; // etc

    The problem with having an array store a variable (not known at compile time) amount of data is solved by the vector. The vector will resize itself if you put more in than it can hold, an array will overflow. You could use dynamic memory (new) and pointers, but that is messy and error prone, and eventually you'd probably end up creating something similair to vector if you wanted to do it cleanly anyway.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    um...you didn't explain how to read each line into an array without knowing how many lines there are in the text file...i think
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ...anybody?
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  9. #9
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    That is because vector is a substitute array, a better array essentially. It will act very much like an array, but have greater functionality. Anything you wanted to do with an array you can do with a vector.
    Last edited by SilentStrike; 11-14-2001 at 06:04 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    in what variable is each line stored?
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  11. #11
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Each line of the file is stored in Lines. For a file with n lines, the first line is at Lines[0], the second line at Lines[1], the nth (last) line at Lines[n-1].
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  12. #12
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    would i be able to do soemthing like the following?
    Code:
    int Id;
    Id = lines[0];
    
    string Name;
    Name = lines[1];
    
    cout << "ID = ";
    cout << Id;
    cout << "\nName = ";
    cout << Name;
    where in the file it had:
    Code:
    6
    Chris
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  13. #13
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    int Id;
    Id = lines[0];
    No, because lines[0] is a string, and you want to assign it to an int.

    You can get an integer value from a string like this

    Code:
    #include <iostream>
    #include <strstream>
    #include <string>
    
    int main(int argc, char *argv[])
    {
      std::string blah="212";
      std::string blah2="32";
    
      std::istrstream inputBlah(blah.c_str());     // make input stream from blah's input, will function like cin
      std::istrstream inputBlah2(blah2.c_str());   // make input stream from blah2's input
    
      int intBlah, intBlah2;
      inputBlah >> intBlah;    // get value of intBlah from the inputBlah stream
      inputBlah2 >> intBlah2;  // get value of intBlah2 from inputBlah2 stream
    
      std::cout << "Integer sum of blah and blah2 is " << intBlah + intBlah2 << std::endl;
      
      char wait; cin >> wait;
      return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  14. #14
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    but is the other one right (the one that get's the sting "Name"? And how would i change the code that I gave you so it gets the Id integer?
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  15. #15
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    int getInt(string& str) {
        istrstream stringInputStream(str.c_str());
        int whatever;
        stringInputString >> whatever;
        return whatever;
    }
    
    int main () {
        // all that other crap
        int Id = getInt(lines[0]);
          
        cout << "ID = ";
        cout << Id;
        cout << "\nName = ";
        cout << lines[1];
        
        return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21