.txt file and arrays

I need to get lines of information from a .txt file and read them into an array or arrays, but I can't seem to find the code I need to accomplish this. I have a file that contains a name three different numbers and another name; john 1 17 5 fred. The file has multiple lines with this data. I need to access the data, search it for a specific line of data, output that data, manipulate the data then put it back in the file. The point is to get players records, find a specific players record, output it, manipulate it, and put it back in. I am so lost someone please help!!!
closed account (3qX21hU5)
What are you lost on? What do you have so far? Need to be a bit more specific as to what you are having trouble with.

Try checking out http://www.cplusplus.com/doc/tutorial/files/ it might have some info to push you in the right direction.
That's just it I don't know where to start, how do I take info from a file and transfer it to an array, and what kind of array do I use? So far I have;
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string line;

ifstream inData;//object to access .txt file
inData.open("PlayerRecord.txt");

if (inData.is_open())
{
while ( inData.good() )
{
getline (inData,line);
cout << line << endl;
}
}
else cout << "Unable to open file";



inData.close();
system("pause");
return 0;
}

This prints it to the screen, but how do I put the information in an array so i can access the right piece?
Last edited on
FYI: Your string line can be accessed like an array using the operator [ ]

However, given the format of a input line as, "string number number number string"

This will read each of the data on a line into their respective variable for easy processing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string name1;
string name2;
long n1;
long n2;
long n3;

if( inData.is_open() )
{
   while ( inData.good() )
   {
      inData >> name1 >> n1 >> n2 >> n3 >> name2;
      // process data and write it back to another file
  }
}


If a number can have a decimal, replace long with float or double.

--
Rajinder Yadav
DevMentor Labs <labs@devmentor.org>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
I have a file that contains a name three different numbers and another name; john 1 17 5 fred.

If the data is very well-defined and will always fit that description, I would use a structure, maybe like this:

1
2
3
4
5
6
7
8
struct row 
{
    string name1;
    int num1;
    int num2;
    int num3;
    string name2;
}


and then define an array (or vector) of these objects row array[1000];


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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

struct row
{
    string name1;
    int num1;
    int num2;
    int num3;
    string name2;
};

int main()
{
    string line;

    ifstream inData;//object to access .txt file
    inData.open("PlayerRecord.txt");

    if (!inData.is_open())
    {
        cout << "Unable to open file";
        //system("pause");
        return 1;
    }

    row array[1000];
    int count = 0;
    
    while (getline (inData,line))
    {
        stringstream ss(line);
        ss >> array[count].name1
           >> array[count].num1
           >> array[count].num2
           >> array[count].num3
           >> array[count].name2;
        count++;
    }

    inData.close();

    for (int i=0; i<count; i++)
    {
        cout << array[i].name1 << ", "
             << array[i].num1  << ", "
             << array[i].num2  << ", "
             << array[i].num3  << ", "
             << array[i].name2 << endl;
    }

    //system("pause");
    return 0;
}

Output:
john, 1, 17, 5, fred
jane, 2, 32, 12, lucy
mike, 234, 23, 24, joe


Last edited on
@ Shervil ..
Instead of writing system("Pause");
write cin.ignore();

I don't know why people say that.. but it has something to do with the security and besides it tags you as an absolute noob.
This post is not realted to the actual questions ..
Actually I never use system("Pause");, I left it there out of politeness to the OP.
Thank you for the great tips everyone, I will try to implement them later today and post my results. I use system("pause") to see what I am doing, I usually put it at the end of my main. I am still new so this helps me see the output.
I ended up using a struct as suggested, Thank you for the information you guys sure helped me figure out that small but crucial part of my final project.
Topic archived. No new replies allowed.