C++ text file line by line/each line to string/array
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C++ text file line by line/each line to string/array
Hello,
I need help in C++
I am trying to write a program but I am stuck as I have to read a serial text file .txt which is like this:
1 2 3 4 5 6 7 .....till 100 but can also be less
23 34 34 5 4 54 ...till the number of the first line
The first line is like the ID and the other one the score (or anything) of each ID.
So what I need to know is how to read a text file line by line and put each line to an array which I don't know it's length...actually that's a string.
The only ways I know of(maybe wrong)is to read the whole file into a buffer add to it and rewrite the whole file.
or
read a line in append to it, write the new line to another file until to have written all the data plus what you want to append to it.
reading non-integer values from a table in a text file
Hi, everyone
I have a similar problem, but with non-integer numbers
I would like to read a table like this, from a txt file. The first line represents the x_values of the table, the second line provides the y_values of the table, and then it follows the z_values for the different x and y values
the problem is that I need to read and save this kind of information from different files, which will not always have the same dimensions
Could anyone help me, please?
Thanks so much
I have modified the file provided by dakensta on the 12-18-03 at 01:40 AM and it works fine with Microsoft VISUAL C++
But it does not work with Open Watcom compiler :-( It seems that the getline() function is not recognized...
Has anyone faced this problem before? How can I solve it? Thanks so much !!!
Last edited by new_cpp_programmer; 05-03-2007 at 02:17 AM.
Hi, I have written a similar program using STL.
Here are the details of my program. It reads a text file and then asks the user to enter a string.
It then displays how many times the string occurs and also prints the lines in which it occurs.
I hope it solves your purpose ?
So here's the code :
Code:
#include<map>
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::map;
using std::multimap;
int main()
{
multimap<string,int> words;
map<int,string> lines;
string str;
ifstream input("test.txt");
if(input.fail())
{
cerr<<"\nThe file could not be opened.";
return -1;
}
int i=1;
while(getline(input,str))
{
istringstream in(str);
string s;
while(in>>s)
{
words.insert(make_pair(s,i));
}
lines.insert(make_pair(i,str));
i++;
}
string search;
cout<<"\nEnter a word to search: ";
cin>>search;
cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
multimap<string,int>::iterator it1=words.lower_bound(search);
multimap<string,int>::iterator it2=words.upper_bound(search);
while(it1!=it2)
{
int x=it1->second;
map<int,string>::iterator iter=lines.find(x);
cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
it1++;
while(true)
{
if(it1!=it2 && it1->second==x)
{
it1++;
}
else
{
break;
}
}
}
return 0;
}
What the code does is that it stores each line number and line in a map and each word and its associated line number in a multimap(because each line may contain more than 1 similar words)
Rest is simple( i guess ?) :P
I usually have a hard time explaining things:-(, hope you'll figure out
the rest.
hi thanks for the help!!! i have another problem in c++
its about the tower of hanoi... sad it say its my first
time to encounter this problem... i need help again..
Efficient way to read only 1st line from number of files
Hi,
I am struck up with some problem. I am analyzing on an efficient way to read number of files. Actually I want to read only the first line from each of them.
There could be hundreds of them in the directory/sub-directories; but not huge in size.
Can anyone suggest a way I can do it without compromising too much on memory and performance.
using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::map;
using std::multimap;
int main()
{
multimap<string,int> words;
map<int,string> lines;
string str;
ifstream input("test.txt"); //create file for input
if(input.fail())
{
cerr<<"\nThe file could not be opened.";
return -1;
}
int i=1;
while(getline(input,str))
{
istringstream in(str);
string s;
while(in>>s)
{
words.insert(make_pair(s,i));
}
lines.insert(make_pair(i,str));
i++;
}
string search;
cout<<"\nEnter a word to search: ";
cin>>search;
cout<<"\nThe number of matches = "<<words.count(search)<<'\n';
multimap<string,int>::iterator it1=words.lower_bound(search);
multimap<string,int>::iterator it2=words.upper_bound(search);
while(it1!=it2)
{
int x=it1->second;
map<int,string>::iterator iter=lines.find(x);
cout<<'\n'<<x<<" ) "<<iter->second<<'\n';
it1++;
while(true)
{
if(it1!=it2 && it1->second==x)
{
it1++;
My god, this thread is the walking dead! The things I would do if I knew how to come back to life 4 times
lppc2112: Please create a new thread with more specific information about what you are trying to do and please enclose your code in [code]...[/code]. Thank you.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.