0

I have a file (Existingfile.txt)that contains details of food items as shown:

  1. Pasta 1.99
  2. Cheese cake 4.99

and so on. I have to write a c++ program that reads these records. Can I create a structure as follows to read the records like individual structures?

struct food
{
int no;
string name;
float cost;
}s;

//TO read from file

void read(){
fstream f;
f.open("Existingfile.txt",ios::in);
while(!f.eof()){
f.read((char*)&s,sizeof(s));
.
.
.

}
.
.
.
f.close();

}

P.S: I am new to stackoverflow. If my question is not correct, please comment and I will clear it. Please guys, I need your help!

ShankRam
  • 314
  • 2
  • 13
  • Yes, that is possible but not directly into your struct. You can read it into a string byt parsing that is not particularly easy for this format. Maybe `fscanf` can help you. Are you flexible in the file format or is it given? – CompuChip Nov 22 '15 at 06:06
  • The simple answer is no, not in the way I think you mean. A problem is that your schema (such as it is) is: 1) an index as an integer 2) a period 3) a series of blank separated words that is terminated by a price 4) the price. But the struct you have doesn't have that much information. For example the struct has no way to "know" that the first character of the price "10.99" is not part of the string the precedes the price. You'll need to write some simple parsing code. – Χpẘ Nov 22 '15 at 06:10
  • @CompuChip I have flexibility in the file format. It is not given. – ShankRam Nov 22 '15 at 08:45
  • @user2460798 I can alter the file so as to remove the period. As to the name of the food items, can I add '\0' at the end, so the string will terminate when it encounters '\0'. Then will it be possible? – ShankRam Nov 22 '15 at 08:48

4 Answers4

0

You can, but it pretty complicated if you use text like that because it format. The easy way is using delimiter so it can be split by a delimiter (space for example). For example easy text:

1;Pasta;300
2;Chess cake;400

you can split it by ; character. Or if you are using space as your delimiter then you can just use input stream for that.

instream >> num >> food >> total;

but your text must not contain space except for delimiter.

Hernantas
  • 341
  • 1
  • 6
0

Have you tried compiling it? if all the elements of the structure were fixed size of c-style-strings, and if the length of the input line were fixed, it could have worked.

However, you can't read like that because the elements are not basically chars. for example, cost is an float. If you read '19.99'. it would be just a string "19.99, not a float value 19.99.

I recommend you to read a line, save it to a string buffer, and you can code to parse the information. or just use fscanf

Y.C.Jung
  • 53
  • 6
  • The code I have mentioned in my question is part of a small program which goes like this: I display all the items to the user. The user enters the item no. and it is added to his cart. I want to use structure because it will be easy to compare the item no. the user enters to the file and find the cost and name of the item. I will use the following piece of code for that: (in next comment) – ShankRam Nov 22 '15 at 08:53
  • @Compuchip (please read my previous comment and check this out): http://codepad.org/mVN5RAT6 – ShankRam Nov 22 '15 at 09:08
0

I think that there is an easier way to store your data using a hashtable. Take a look at this link to figure out how to implement it. link1

Alternatively, you also can use an array to do so. link2

Community
  • 1
  • 1
Tung Le
  • 109
  • 1
  • 3
  • 11
  • The problem with your idea is that the entire list of items(it is quite huge) will be stored in the program(I guess). I don't want my program to be very large in size. Which is why I want the info to be only in the file and I can display it whenever I want. Please correct me if I am wrong – ShankRam Nov 22 '15 at 08:52
  • You are right. So, you can create a struct and put each line of the file into a vector of 1xn dimension, where each row of the vector will store each line of the file. You can see the reference [here](http://stackoverflow.com/questions/18159820/how-to-initialize-an-array-of-vectorint-in-c-with-predefined-counts). – Tung Le Nov 23 '15 at 02:23
0
ifstream ob("text.txt")
string record ; 
 string temp ; 

while(getline(ob, record))
{
   stringstream ss(record); 
ss>>s.no>>s.cost ; 


}
Shivam Gupta
  • 77
  • 1
  • 8
  • 1
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Nov 22 '15 at 10:31