I/O with lines of a file

Nov 2, 2011 at 9:59pm
I'm making a ballet for my gov. teacher and I need to know how to get independent lines from a text file as variables because I want my program to be able to be flexible with candidate names. You can change the number and names of the candidates and it writes the candidate names to a text file.
here's how the text file will look like:

Candidate 1
Candidate 2
Candidate 3

and so forth.

My problem is, if i just regularly grab until eof() has been reached, we just have a long line of letters that we have to read. It would look like this:

Candidate1Candidate2Candidate3

so, how to get a line of text, and then move to the next and get that line. Ultimately, this will be used to display the Ballot for input until a user presses a special key that isn't displayed onscreen (for control purposes, we dont want voters to exit the vote screen). That aside, the vote menu needs to display the variable candidate names. This means somthing like an array would be a good approach, but I'm don't entirely understand how arrays work with strings... so... yeah.
Last edited on Nov 2, 2011 at 10:00pm
Nov 6, 2011 at 11:21pm
I know it's getline(fileHandle, stringvariable), but i dont know how to make it go to the next line.
Nov 7, 2011 at 11:31pm
to be more specific, i would like to actually get a line. for example, if line 3 of the file is designate a variable on the options menu (wich would be written only to that line "on" or "off" depending on the option selected) as some option, I want to be able to write specifically to that line, and get information specifically from that line. Because what's written on the line varies, we cant just match what's on the line to some sort of static text.
Nov 8, 2011 at 10:55pm
so.....

i searched and couldnt find anything specific to what i wanted... that's why i posted this here a week ago...
Nov 8, 2011 at 11:08pm
getline automatically moves the file's get pointer to the next line, so keep calling getline until eof() returns true. Just load the strings into a vector<string> and use the [] operator to access specific candidates.
Nov 9, 2011 at 1:55am
hmm... ok. Ill try that. But ive read up on vectors and i dont quite understand how they work... Can you explain them to me so I understand?
Nov 9, 2011 at 2:09am
To understand how vector work as in syntax-wise? Then I think you need a thorough understanding on C++ templates. And how this C++ template feature is used to maximum effect in the STL which include the vector class you are going to use.
Nov 9, 2011 at 2:16am
@sohguanh

that doesnt help me...
Nov 9, 2011 at 2:24am
so i took modshop's advice and made a "test" program. It kind of messes up on itself... Anyone care to tell me the problem? That would really help! :)

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>
#include <windows.h>

using namespace std;

int opnfile()
{
    Sleep(10);
    while(_kbhit()) _getch();     // clears the getch() stream
    vector<string> lines;
    string txt;
    int x, lin;
    x = 0;
    ofstream gro;   // opens and writes data top a file for testing
    gro.open("groceries.txt", ios::out);
    if (gro.is_open())
    {
        gro<< " This is the grocery list"<< endl;
        gro<< ""<< endl;
        gro<< " 1- tomatoes and starch"<< endl;
        gro<< " 2- potatoes and beans x2"<< endl;
        gro<< " 3- this is line 3 i guess..."<< endl;
        gro<< " 4- tomatoe tomahtoe"<< endl;
        gro<< ""<< endl;
        gro<< ""<< endl;
        gro<< "_________________________________________"<< endl;
        gro.close();
    }
    //now we want to open the file and get every line and put it into a vector
    ifstream gro2;
    gro2.open("groceries.txt", ios::in);
    if (gro2.is_open())
    {
        while(!gro2.eof())
        {
            x++;                      //x countes the number of lines in the file
            getline(gro2, txt);
            lines[x] = txt;
        }
    }
    system("CLS");
    cout<< "Which line do you want to see?"<< endl;
    cout<< ""<< endl;
    cin>> lin;
    txt = lines[lin];
    cout<< txt<< endl;
    cout<< ""<< endl;
    system("PAUSE");
    return 0;
}

int main()
{
    char ch;
    int x;
    x = 0;
    while (x == 0)
    {
        cout<< "                  MENU"<< endl;
        cout<< ""<< endl;
        cout<< ""<< endl;
        cout<< " 1- display a file"<< endl;
        cout<< " 2- quit"<< endl;
        cout<< "___________________________________________________"<< endl;
        ch = _getch();
        ch = toupper( ch );
        if (ch == '1')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            opnfile();
            continue;
        }
        if (ch == '2')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            return 0;
        }
    }
}


it's a small program i designed to see if i could get this, obviously not... :( Please help me.
Nov 9, 2011 at 2:45am
ok, i did some searching and it's apparent that like arrays, vectors need to be declared with a size, so i re-wrote the program to get the number of lines in the file, and create a vector that is the size of the file. Here is is:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>
#include <windows.h>

using namespace std;

int opnfile()
{
    system("CLS");
    Sleep(10);
    while(_kbhit()) _getch();     // clears the getch() stream
    string txt;
    int x, lin;
    x = 0;
    ofstream gro;   // opens and writes data top a file for testing
    gro.open("groceries.txt", ios::out);
    if (gro.is_open())
    {
        gro<< " This is the grocery list"<< endl;
        gro<< ""<< endl;
        gro<< " 1- tomatoes and starch"<< endl;
        gro<< " 2- potatoes and beans x2"<< endl;
        gro<< " 3- this is line 3 i guess..."<< endl;
        gro<< " 4- tomatoe tomahtoe"<< endl;
        gro<< ""<< endl;
        gro<< ""<< endl;
        gro<< "_________________________________________"<< endl;
        gro.close();
    }
    //counts the number of lines in the file, and creates a vector of the file's size
    ifstream cnt;
    cnt.open("groceries.txt", ios::in);
    if (cnt.is_open());
    {
        x = 0;
        while(!cnt.eof())
        {
            x++;
            getline(cnt, txt);
        }
    }
    vector<string> lines(x);
    x = 0;                                //reverts x back to zero so we can re-use it
    //now we want to open the file and get every line and put it into a vector
    ifstream gro2;
    gro2.open("groceries.txt", ios::in);
    if (gro2.is_open())
    {
        while(!gro2.eof())
        {
            x++;                      //x countes the number of lines in the file
            getline(gro2, lines[x]);
        }
    }
    system("CLS");
    cout<< "Which line do you want to see?"<< endl;
    cout<< ""<< endl;
    cin>> lin;
    txt = lines[lin];
    cout<< txt<< endl;
    cout<< ""<< endl;
    system("PAUSE");
    return 0;
}

int main()
{
    char ch;
    int x;
    x = 0;
    while (x == 0)
    {
        cout<< "                  MENU"<< endl;
        cout<< ""<< endl;
        cout<< ""<< endl;
        cout<< " 1- display a file"<< endl;
        cout<< " 2- quit"<< endl;
        cout<< "___________________________________________________"<< endl;
        ch = _getch();
        ch = toupper( ch );
        if (ch == '1')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            opnfile();
            continue;
        }
        if (ch == '2')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            return 0;
        }
    }
}


for some reason it still does the same thing... I dont know why though. I added the "system"cls"" to see if it was going to the function, and it is, but before it displays a query to the user of what line to display, windows say it has to close it because of an error or somthing...

oh, and by the way, im using Windows 7, if that makes any difference. Thank you for your time!
Last edited on Nov 9, 2011 at 2:47am
Nov 9, 2011 at 4:11am
Your over-complicating things a bunch here. Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream file("file.txt");
vector<string> lines;
string cLine; //current line

while (file.good())
{
    getline(file, cLine); //get from file
    lines.push_back(cLine); //add to the end of the vector
}

//to access vector
for (int i = 0; i<lines.size(); i++)
{
    cout << lines[i] << endl;
}


EDIT: Fixed typos
Last edited on Nov 12, 2011 at 1:55am
Nov 9, 2011 at 2:04pm
so, if i wanted to access a specific line, i would input a number, and putr it into a variable, ( such as i) and get it from the vector? Cool! thanks!
Nov 12, 2011 at 1:24am
i tried it and it doesnt work. The compiler says that there is an error with it.
Last edited on Nov 12, 2011 at 1:25am
Nov 12, 2011 at 1:58am
My code? There were some typos, I just fixed them.
Nov 12, 2011 at 7:05pm
... still. does not work. Here is the errors:

vector test\opening files with vector.cpp||In function 'int opnfile()':|
vector test\opening files with vector.cpp|45|error: request for member 'push_back' in 'lines', which is of non-class type 'std::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> > > >()'|
vector test\opening files with vector.cpp|52|warning: pointer to a function used in arithmetic|
vector test\opening files with vector.cpp|52|error: invalid conversion from 'std::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> > > > (*)()' to 'char'|
vector test\opening files with vector.cpp|52|error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
||=== Build finished: 3 errors, 1 warnings ===|


here is my code:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>
#include <windows.h>

using namespace std;

int opnfile()
{
    system("CLS");
    Sleep(10);
    while(_kbhit()) _getch();     // clears the getch() stream
    vector<string> lines();
    string txt;
    int x, lin;
    x = 0;
    ofstream gro;   // opens and writes data top a file for testing
    gro.open("groceries.txt", ios::out);
    if (gro.is_open())
    {
        gro<< " This is the grocery list"<< endl;
        gro<< ""<< endl;
        gro<< " 1- tomatoes and starch"<< endl;
        gro<< " 2- potatoes and beans x2"<< endl;
        gro<< " 3- this is line 3 i guess..."<< endl;
        gro<< " 4- tomatoe tomahtoe"<< endl;
        gro<< ""<< endl;
        gro<< ""<< endl;
        gro<< "_________________________________________"<< endl;
        gro.close();
    }
    else
    {
        gro.close();
    }
    ifstream vc;             //we will open the file and get each line as a vector
    vc.open("groceries.txt", ios::in);
    if (vc.is_open())
    {
        while(vc.good())
        {
            getline(vc, txt);
            lines.push_back(txt);
        }
    }
    system("CLS");
    cout<< "Which line do you want to see?"<< endl;
    cout<< ""<< endl;
    cin>> lin;
    txt = lines[lin];
    cout<< txt<< endl;
    cout<< ""<< endl;
    system("PAUSE");
    return 0;
}

int main()
{
    char ch;
    int x;
    x = 0;
    while (x == 0)
    {
        cout<< "                  MENU"<< endl;
        cout<< ""<< endl;
        cout<< ""<< endl;
        cout<< " 1- display a file"<< endl;
        cout<< " 2- quit"<< endl;
        cout<< "___________________________________________________"<< endl;
        ch = _getch();
        ch = toupper( ch );
        if (ch == '1')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            opnfile();
            continue;
        }
        if (ch == '2')
        {
            Sleep(10);
            while(_kbhit()) _getch();
            return 0;
        }
    }
}


i dont know whats wrong...
Last edited on Nov 12, 2011 at 7:06pm
Nov 16, 2011 at 10:36pm
...
Nov 18, 2011 at 12:42am
You don't need the parenthesis on line 15 after you declare "lines". You only need them if your calling a constructor that takes parameters.
Nov 18, 2011 at 10:35pm
wow! cool that helped! it works perfectly now. I appreciate your time.
Topic archived. No new replies allowed.