+ 2
Searching for a line in file c++
Hello everyone! I've got a file with text, like 0001 hello 0.67 0002 hellow 0.55 and ive got a string with text, for example "hello". The program should search for this word in a file, and return a value after this word and line's number, and write this values to variables (in this example, program will return 0001 and 0.67) and i dont know how to do it :-) i can write all file to string or array, but what should i do next... p.s. sorry for my bad english :)
6 Réponses
+ 5
"sorry for my bad english :)"
Like always your English is awesome and very interesting!
~~~~~~~
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string mass;
ifstream fs("memory.txt");
if (!fs) return 1;
while (getline(fs, mass))
{
cout << mass << endl;
}
fs.close();
return 0;
}
Outout:
0001 hello 0.67
0002 hellow 0.55
+ 2
The Question is: did you tried to find an own Solution for this Problem? It is always better to write a Code by yourself. If it won't work you can post it here and the Community will help you to fix it. But you should not wait that anyone here does the Job for you.
+ 2
Oh, okey, I understood. Thank you! Yes, I tried this one. It works and lines are in array, but the problem is that I dont know what to do with this result to solve my task.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const int len = 30, strings = 2;
const char ch = '\n';
char mass[len][strings];
ifstream fs("/storage/emulated/0/C++/memory.txt",);
if(!fs) return 1;
for(int r = 0; r<strings; r++)
{
fs.getline(mass[r], len-1,ch);
cout << mass[r] << endl;
}
fs.close();
return 0;
}
+ 1
Thank's for your Question. It sounds like an interesting Task. But please show your attempt to get Help from the Community.
+ 1
Wow, that was easier that i thought! Thank you!
0
sorry, may be i didnt understand you, but what does your last sentence mean?)