Querry!!
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
I believe this query was answered
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
-
I couldn't get that. Plesae elaborate the clue so that i can learn a bit more. I am trying to use the "isdigit" command for this purpose. Would that be suitable?
Dear Razanust, I'll first suggess you not to create a new thread if you aren't satisfied with the answer.
Razanust wrote:
couldn't get that. Plesae elaborate the clue so that i can learn a bit more. I am trying to use the "isdigit" command for this purpose. Would that be suitable?
Now, as your file is a text file, :confused: First Clear whether you have stored it by formating by sprintf and then WriteFile etc or Writing the structure directly? HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
modified on Tuesday, June 30, 2009 3:59 AM
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
Razanust wrote:
Any clue to the solution???
1. Read the file line by line 2. use something like
strtok
to tokenize each line 3. fill the structure -
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
You need to open the file, read the contents, parse the read contents, store them into an array of structures. Which part of this exercise are you having a problem with? And what have you done so far?
It is a crappy thing, but it's life -^ Carlo Pallini
-
Razanust wrote:
Any clue to the solution???
1. Read the file line by line 2. use something like
strtok
to tokenize each line 3. fill the structureI Don't know why u are trying there tipical methods. Just Try the "C" way: -
struct MyStruct { char Name\[20\]; int age; } // Fill the Structure with the appropriate values MyStruct myStruct; FILE \*pFile = fopen("Record.txt", "Wb+"); fwrite((char\*)&myStruct, sizeof(MyStruct), 1, pFile);
and to reead :-
fread((char*)&myStruct, sizeof(MyStruct), 1, pFile);
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
-
I Don't know why u are trying there tipical methods. Just Try the "C" way: -
struct MyStruct { char Name\[20\]; int age; } // Fill the Structure with the appropriate values MyStruct myStruct; FILE \*pFile = fopen("Record.txt", "Wb+"); fwrite((char\*)&myStruct, sizeof(MyStruct), 1, pFile);
and to reead :-
fread((char*)&myStruct, sizeof(MyStruct), 1, pFile);
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
Harsh Shankar wrote:
I Don't know why u are trying there tipical methods.
What?
-
Harsh Shankar wrote:
I Don't know why u are trying there tipical methods.
What?
Wouldn't it be easier to read and write the data in the file by my given way??
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
modified on Tuesday, June 30, 2009 5:25 AM
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
-
Wouldn't it be easier to read and write the data in the file by my given way??
HARSH Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning.
modified on Tuesday, June 30, 2009 5:25 AM
I don't see anything in his post about writing the data back to a file. He only wants to read the file and store the tokens in a structure (or presumably an array of structures).
-
I have names of certain students and their ages stored in a text file 'records.txt'. LIKE: John 22 Paul 21 Sam 24 Harry 23 Now i want to store these ages and names in a structure 'records(r)' directly from the file. (Here i am not taking any input from the user) as r.name & r.age. Any clue to the solution???
Razanust wrote:
Any clue to the solution???
Well, something like this comes to mind:
std::ifstream fin;
std::vector<Record> records;fin.open("c:\\Records.txt");
std::copy(std::istream_iterator<Record>(fin), std::istream_iterator<Record>(), std::back_inserter(records));
fin.close();Hint: you'll need to create the
Record
class. ;)"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons