LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Closed Thread
 
Search this Thread
Old 12-16-2003, 09:06 AM   #1
Dimitris
LQ Newbie
 
Registered: Aug 2003
Posts: 13

Rep: Reputation: 0
Post 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.

Any help would be really helpfull !

Regards,
Dimitris
 
Old 12-16-2003, 11:19 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Just off the top of my head, I'd say, use ifstream::getline and an istrstream class.

Here's a quick app as an example.

Code:
#include <iostream>
#include <fstream>
#include <strstream>

using namespace std;

int main()
{
	char buffer1[2048];
	char buffer2[2048];
	istrstream ostr1(buffer1, 2048);
	istrstream ostr2(buffer2, 2048);
	int values1[100];
	int values2[100];
	int c=0;

	ifstream fin("data.txt");

	fin.getline(buffer1, 2048);
	fin.getline(buffer2, 2048);

	while (ostr1 >> values1[c])
	{
		ostr2 >> values2[c++];
	}

	for (int i=0;i<c;i++)
	{
		cout << values1[i] << ":" << values2[i] << endl;
	}

	return 0;
}
 
Old 12-17-2003, 06:40 PM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Code:
#include <sstream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

void put_into_vector( ifstream& ifs, vector<int>& v )
{
  string s;
  getline( ifs, s );
  istringstream iss( s );
  // not the fastest method ... 
  copy( istream_iterator<int>( iss ), istream_iterator<int>(), back_inserter( v ) );
}

int main()
{
  vector<int> line_1, line_2;

  ifstream ifs( "data.txt" );

  put_into_vector( ifs, line_1 );
  put_into_vector( ifs, line_2 );

}
note: istrstream is deprecated, istringstream is the current class to use.

Post this on gamedev, btw?

Last edited by dakensta; 12-17-2003 at 06:42 PM.
 
Old 12-17-2003, 06:49 PM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Or if you only want to read a text file;

Code:
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
  vector<string> text_file;

  ifstream ifs( "data.txt" );
  string temp;

  while( getline( ifs, temp ) )
     text_file.push_back( temp );
}
 
Old 12-19-2003, 05:38 AM   #5
Dimitris
LQ Newbie
 
Registered: Aug 2003
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks alot for your replies!
I really appreciate it, and yes I did post it there too.

Have a nice day,
 
Old 11-29-2006, 09:25 PM   #6
mani_um
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
C++ read first line

hi sir wanna ask u question

anyone can help how to read and modify only first row without read all the lines for example

This is input file named as test.dat

HIHOW R U 233434 45456564767Y
skskddfggkfgkfg dfdklgfglfl;g
dfk;l 24545 g54545465454544
gfgfgf54 1214


i want a c++ program to read the file line and append number at back
for example add 1 at back like this
"HIHOW R U 233434 45456564767Y"

TO

"HIHOW R U 233434 45456564767Y1"


any one can help with this

thanks in advance

regards,
mani
 
Old 11-29-2006, 10:08 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 11-29-2006 at 10:09 PM.
 
Old 05-02-2007, 05:18 PM   #8
new_cpp_programmer
LQ Newbie
 
Registered: May 2007
Posts: 1

Rep: Reputation: 0
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

0.5 0.89 0.95 1.5 3.0 8.0 22.5
0.0 5.0 10
1.12 1.13 1.15 1.18 2.4 3.67 6.9
2.12 2.13 2.15 2.18 5.4 3.67 6.9
3.12 3.13 3.15 3.18 3.4 4.67 8.9

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.
 
Old 12-03-2007, 06:45 PM   #9
atngalbreaker
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
hi guys

we have a project in c++ that includes dna codes.

we need to read the whole dna code in the text file to see how many times
our inputed code occurred and in what location it occurred.

for example we inputed ggccgg

the program will then read from the text file to see how many times it occurred and its location.

the problem is i forgot how to read from a text file..huhu

i need your help guys thanks
 
Old 12-04-2007, 03:19 AM   #10
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
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.

Last edited by lali.p; 12-04-2007 at 03:20 AM.
 
Old 12-11-2007, 12:07 AM   #11
atngalbreaker
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
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..
 
Old 12-11-2007, 02:08 AM   #12
lali.p
Member
 
Registered: Jan 2007
Distribution: Slackware 11.0
Posts: 141

Rep: Reputation: 16
ooops your home work !!!

Dude seems like you are posting your homework over here. *BAD THING*

Anyway, in case you may be in a situation where you want the solution.
Here's it :
Code:
#include<iostream>
using std::cout;
using std::cin;
void hanoi(int,int,int,int);
void move(int,int);
int main()
{
	int plates=2;
	int from=1,to=2,via=3;
	hanoi(plates,from,to,via);
	return 0;
}
void move(int from,int to)
{
	cout<<'\n'<<from<<" "<<to;
}
void hanoi(int plates,int from,int to,int via)
{
	if(plates<0)
	{
		return;
	}
	hanoi(plates-1,from,via,to);
	move(from,to);
	hanoi(plates-1,via,to,from);
}
Please note that you won't gain anything by copy pasting anything.
You gonna learn only by yourself. I hope you are not cheating yourself.
 
Old 03-05-2008, 03:27 AM   #13
anurade
LQ Newbie
 
Registered: Mar 2008
Posts: 2

Rep: Reputation: 0
Question 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.

Thanks in advance.

/Anu
 
Old 03-10-2008, 12:13 PM   #14
lppc2112
LQ Newbie
 
Registered: Mar 2008
Posts: 7

Rep: Reputation: 0
Stl program

Hello,

I have to use data structures an array or vector to store vertices in this program.

can anyone tell me how i can do it please?

#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"); //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++;

}
else
{
break;
}
}
}

cout<<'\n';

return 0;
}
 
Old 03-10-2008, 08:06 PM   #15
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 03:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration