LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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



Reply
 
Search this Thread
Old 12-27-2006, 01:29 PM   #1
balgaltz
LQ Newbie
 
Registered: Dec 2006
Posts: 1

Rep: Reputation: 0
C++ Generating 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( "C://data.txt" );

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

}

Is there a way to read automatically each line of the data.txt depending on how many lines the file has?

I can get the number of lines that it has with:

int line_count = 0;
char ch;

ifstream iFile("c:/data.txt");

if (! iFile)
{
cout << "Error opening input file" << endl;
system("pause");
return -1;
}

while (iFile.get(ch))
{
switch (ch) {
case '\n':
line_count++;
break; }
cout << line_count;

Thanks in advance
 
Old 12-28-2006, 11:16 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
You could always test to make sure that you're not at the EOF. I don't know how to do this in C or C++, but it's done like so in a few other languages:
Code:
Java
java.util.Scanner s = new java.util.Scanner(new java.io.File("file.txt"));
String str;
while(s.hasNext() == true) {
  System.out.println(s.nextLine());
}

BASIC
OPEN "file.txt" FOR READ AS #1
WHILE NOT EOF(1)
  INPUT #1, s$
  PRINT s$
WEND
CLOSE #1

Perl
open(INFILE, "file.txt");
while(<INFILE>) {
  print("$_\n");
}
close(INFILE);

Lisp
(with-open-file (istream #p"file.txt" :direction input)
  (format t "~a~&" (read-line istream)))
I hope this helps in some way.
 
Old 12-28-2006, 11:18 PM   #3
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
It's similar in C++ as well:

Code:
if (!fstream.eof())
{
    // Do Stuff Here.
}
Regards,

Alunduil
 
Old 12-28-2006, 11:21 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Good to know.
 
  


Reply


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
generating mips assembly from c code with gcc? lilili Programming 4 05-11-2006 09:37 PM
/etc/profile (need help re-generating) JesseMor Slackware 1 01-26-2006 04:46 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 02:37 AM
Generating A Script gizza23 Linux - Software 7 07-24-2005 08:33 PM
code for generating random samples! aru_04 Programming 2 07-21-2005 09:35 AM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
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