Extracting string from txt file

Hello how can i extract a string from a txt file?

1
2
3
4
5
6
7
   while (!infile.eof())
        {
             infile >> productID[c] ;
             //assume that infile.getline(productName[c],29) (is this possible? Because it's not working for me.)
             infile >> Price[c];
             c++;
        }
Last edited on
After line 3 infile >> productID[c] ; there is a trailing newline character still left in the infile buffer. You need to get rid of that '\n', maybe by infile.get(), or infile.ignore(1000, '\n');

Oh and it's not a good idea to use !eof() in the condition of a while loop. Even if it appears to work, sooner or later it will let you down. For one thing, it is checking the file status before each access, when it should be checked after the access. The other issue is that there are many other error conditions which may arise, in addition to eof().
What should i use instead of !infile.eof()?
Last edited on
while( infile >> productId[ c ] ) { ... }
What should i use instead of !infile.eof()?

I gave an example here:
http://www.cplusplus.com/forum/beginner/117908/#msg643029
It's not working for me
1
2
3
4
5
6
7
   while (infile >> objectProduct.productID[c])
        {
            infile.get();
            getline(infile, objectProduct.productName[c]);
            infile >> objectProduct.Price[c];
            c++;
        }

the error is "no matching function call to getline(. . . .)
There are two different versions of getline(). The syntax is different depending on whether you use c-strings (a null-terminated array of characters), or the C++ std::string class.

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/string/string/getline/

I tried changing the type from char to string and vice versa but in the when i use the char array it gives me this. Iwas using infile.getline in char array


invalid conversion from char to std:: basic_istream:: . . .
Last edited on
It's hard to diagnose the problem from a fragment of code. If you post a complete program showing:
which headers you include
how the variables are defined
as well as the line of code where getline is used, it would help.
Oh, sorry.

Oufile Part:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    int productID=1;
    string productName;
    double Price;

    ofstream outfile("Productfile.txt");
    if (outfile.is_open())
    {

        while (productID!=0)
        {
        cout << "Enter Product ID: ";
        cin >> productID;
        cin.get();
        cout << "Enter Product name: ";
        getline(cin,productName);
        cout << "Enter Price: ";
        cin >> Price;
        cout << endl;
        if (Price > 0)
        {
            outfile << productID << endl << productName << endl << Price << endl;
        }
        }
        outfile.close();
    }
    else
    {
        cout << "File not open." << endl;
    }
    return 0;
}



infile Part:

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

using namespace std;

int Search(int Searcharray[], int sizeofArray, int searchValue);

struct Product
{
    string productName;
    int productID[20];
    double Price[20];
};

int main ()
{
    int userValue;
    int c=0;
    Product objectProduct;

    ifstream infile("Productfile.txt");
    if (infile.is_open())
    {
        while (infile >> objectProduct.productID[c])
        {
            infile.get();
            getline(objectProduct.productName[c]);
            infile >> objectProduct.Price[c];
            c++;
        }
      cout << "Enter Product ID to find out it's price: ";
      cin >> userValue;
      int result = Search(objectProduct.productID, c, userValue);
      if (result>=0)
      {
          cout << "The Product ID you entered is: " << objectProduct.productID[c] << endl;
          cout << "Product name: " << objectProduct.productName[c] << endl;
          cout << "The price is: " << objectProduct.Price[c] << endl;
      }
      else
      {
          cout << "Product " << userValue << " not found "<< endl;
      }

    }
    else
    {
        cout << "File not open" << endl;
    }
}

int Search(int Searcharray[], int sizeofArray, int searchValue)
{
    for (int i=0; i < sizeofArray; i++)
    {
        if (searchValue == Searcharray[i])
        {
            return i;
        }
    }
    return -1;
}

Last edited on
The input code doesn't really match the data file.
A product has three attributes:
an ID
a name
a price.

However this structure says that a single product will have
one name, twenty IDs and twenty prices.
1
2
3
4
5
6
struct Product
{
    string productName;
    int productID[20];
    double Price[20];
};


the getline() here:
getline(objectProduct.productName[c]);
is trying to read a whole line of text into productName[c] which is a single character, it cannot hold more than one letter. It is also wrong as it does not specify that it should use the infile stream.

The above issues are related. Rather than the product struct having arrays inside it, there should be an array of products. Like this for example:
1
2
3
4
5
6
struct Product
{
    string productName;
    int productID;
    double Price;
};
Here's the array:
 
    Product Productarray[20];
and the input statements would look like this:
1
2
3
4
5
6
7
        while (infile >> Productarray[c].productID)
        {
            infile.get();
            getline(infile, Productarray[c].productName);
            infile >> Productarray[c].Price;
            c++;
        }


When it comes to the search function, pass it the product array,
 
int Search(Product Searcharray[], int sizeofArray, int searchValue)
and match the searchValue against the Searcharray[i].productID

and so on.

I'm getting a sense of deja vu here, as I already supplied more or less the same code in a previous thread.
Last edited on
I gave up on the structures thing and did this
Thanks btw.

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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int Search(int Searcharray[], int sizeofArray, int searchValue);


int main ()
{
	char productName[30][30];
    int productID[30];
    double Price[30];
    int userValue;
    int c=0;

    ifstream infile("Productfile2.txt");
    if (infile.is_open())
    {
        while (infile >> productID[c])
        {
            infile.ignore();
			infile.getline(productName[c], 29);
            infile >> Price[c];
            c++;
        }
      cout << "Enter Product ID to find out it's price: ";
      cin >> userValue;
      int result = Search(productID, c, userValue);
      if (result>=0)
      {
          cout << "The Product ID you entered is: " << productID[result] << endl;
          cout << "Product name: " << productName[result] << endl;
          cout << "The price is: " << Price[result] << endl;
      }
      else
      {
          cout << "Product " << userValue << " not found "<< endl;
      }

    }
    else
    {
        cout << "File not open" << endl;
    }
	system ("pause>0");
	return 0;
}

int Search(int Searcharray[], int sizeofArray, int searchValue)
{
    for (int i=0; i < sizeofArray; i++)
    {
        if (searchValue == Searcharray[i])
        {
            return i;
        }
    }
    return -1;
}
]
Last edited on
Well, if you got it to work, then that's good. But as for the structures idea, keep it in mind, probably it will come in useful in the future, when you have got a bit more experience.

This is what it looks like (fully working) using the structure:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Product
{
    string productName;
    int productID;
    double Price;
};

int Search(Product Searcharray[], int sizeofArray, int searchValue);

int main ()
{
    int userValue;
    int c=0;
    Product Productarray[20];

    ifstream infile("Productfile.txt");
    if (infile.is_open())
    {
        while (infile >> Productarray[c].productID)
        {
            infile.get();
            getline(infile, Productarray[c].productName);
            infile >> Productarray[c].Price;
            c++;
        }

        cout << "Enter Product ID to find out it's price: ";
        cin >> userValue;
        int result = Search(Productarray, c, userValue);
        if (result>=0)
        {
            cout << "The Product ID you entered is: " << Productarray[result].productID << endl;
            cout << "Product name: " << Productarray[result].productName << endl;
            cout << "The price is: " << Productarray[result].Price << endl;
        }
        else
        {
            cout << "Product " << userValue << " not found "<< endl;
        }
    }
    else
    {
        cout << "File not open" << endl;
    }
    system ("pause>0");
    return 0;
}

int Search(Product Searcharray[], int sizeofArray, int searchValue)
{
    for (int i=0; i < sizeofArray; i++)
    {
        if (searchValue == Searcharray[i].productID)
        {
            return i;
        }
    }
    return -1;
}
Topic archived. No new replies allowed.