First loop is over all lines in file. Second loop is over all numbers in line. I want to be able to do some operations for individual numbers. I considered putting this numbers to some kind of container but it is a really big file. What is the best way of doing it?
Asked
Active
Viewed 248 times
-4
-
can you elabrate your problem and it would be nice if you post your code too – monster Jul 18 '17 at 16:45
-
Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). An MCVE should include a variety of sample input (illustrating all aspects) and desired output. Please show the coding attempts you already made. – Akira Jul 18 '17 at 16:45
-
Define "really big". – molbdnilo Jul 18 '17 at 16:46
-
@monster I don't have any code. I want to write it and I don't know how – P.Musiał Jul 18 '17 at 16:50
-
@molbdnilo 10000 lines, around 15 numbers in each line – P.Musiał Jul 18 '17 at 16:50
-
'I want to be able to do some operations for individual numbers' what operations? Do you really need all 150000 numbers in memory at one time? – Martin James Jul 18 '17 at 16:52
-
Create outer loop to read lines one by one and then inner loop to process one line. When the line is processed then you are in outer loop reading another line. – Marek Vitek Jul 18 '17 at 16:55
-
Probably a duplicate of [Read file line by line](https://stackoverflow.com/questions/7868936/read-file-line-by-line). Answer 1 option 2 should get you going. – user4581301 Jul 18 '17 at 16:58
-
@Martin James No, I don't need to. Operations like inserting to some kind of container or counting an occurrence of a number – P.Musiał Jul 18 '17 at 17:01
-
1The best container to use generally depends on the usage pattern, and you haven't given any information on that. Start with `std::vector` because it's probably the easiest of all options. – user4581301 Jul 18 '17 at 17:01
-
For frequency counts, look at `std::map`. Specifically `std::map
fcount;` because you can easily count with `fcount[numberFromFile]++;` – user4581301 Jul 18 '17 at 17:04 -
@user4581301 In your link there are two variables. I don't know how many variables is in my lines. Besides each line has a different numbers of variables. This file is a database. I want my code to be flexible – P.Musiał Jul 18 '17 at 17:05
-
@user4581301 For example I have 1 4 55 in one line and 12 32 in second line. I want to know that number "32" is in second line on second position – P.Musiał Jul 18 '17 at 17:08
-
Note what Kerrek is doing with the stringstream. You can loop it just like he does with option 1: `while (iss>> a ) { container.push_back(a); }` – user4581301 Jul 18 '17 at 17:09
-
@user4581301 I'll try that. Thank for all comments and voting down :) – P.Musiał Jul 18 '17 at 17:11
-
1@P.Musiał do you want to find in which line a particular number is or line number of each number. in first case you may need no container – monster Jul 18 '17 at 17:12
-
Agree with @monster , but if you must perform multiple queries on the file you will find it much, much easier and faster to read the file and store. Rereading a file is almost always painfully slow compared to reading the file once and then reading from memory. – user4581301 Jul 18 '17 at 17:17
-
@monster This first case is what I want – P.Musiał Jul 18 '17 at 17:19
-
ok i gonna write in answer as the space is too narrow to post as comment – monster Jul 18 '17 at 17:22
-
@P.Musiał 150000 numbers was "really big" in the mid 1980s. In 2017 its smaller than an application's desktop icons. – molbdnilo Jul 18 '17 at 18:36
1 Answers
0
First few steps are same as told in link provided by user4581301 but i am still writing it.
#include<iostream> //habbit
#include<fstream> //for ifstream
#include<sstream> //for istringstream
#include<string> //for getline
using namespace std;
int findnum(int reqNumber){
ifstream infile("thefile.txt");
string line;
int a;
int lineNumber=1;
while (getline(infile,line))
{
istringstream ss(line);
while (ss >> a)
{
if(a==reqNumber)
return lineNumber;
}
lineNumber++;
}
return 0; // result not found in file
}

monster
- 808
- 10
- 23