C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 12th, 2016, 08:32 AM
sychan05 sychan05 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2016
Posts: 1 sychan05 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 20 sec
Reputation Power: 0
Reading only first line from a txt file

Hello friends. I have a problem. Please help me.
1) In a txt file I have 3 properties "name,gender,age", respectively.
Example:
emre boy 23
hasan boy 22
eliza girl 19
I want to take all of the informations from this txt file but with my code i can only take first line
which is "emre boy 23". How can i take the others?. Are there anyone to help me?
------------------------------------------
Code:
#‎include‬ <fstream>
#include <iostream>
#include <string>
using namespace std;
int age;
string name;
string gender;
void importData(char* infileName){
ifstream input(infileName);
input >> name >> gender >> age;
}
void exportData(char* outfileName){
ofstream outFile;
outFile.open(outfileName);
if(name == "emre"){
outFile << age << endl;
}
outFile.close();
}
int main(int argc, char** argv) {
if (argc != 3) {
cout << "Usage: ./project3 infile outfile" << endl ;
return 0;
}
importData(argv[1]);
exportData(argv[2]);
return 0;
}

Reply With Quote
  #2  
Old July 29th, 2016, 02:13 AM
Braids2348 Braids2348 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2016
Location: Earth
Posts: 1 Braids2348 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 47 sec
Reputation Power: 0
End of file (eof)

Quote:
Originally Posted by sychan05
Hello friends. I have a problem. Please help me.
1) In a txt file I have 3 properties "name,gender,age", respectively.
Example:
emre boy 23
hasan boy 22
eliza girl 19
I want to take all of the informations from this txt file but with my code i can only take first line
which is "emre boy 23". How can i take the others?. Are there anyone to help me?
------------------------------------------
Code:
#‎include‬ <fstream>
#include <iostream>
#include <string>
using namespace std;
int age;
string name;
string gender;
void importData(char* infileName){
ifstream input(infileName);
input >> name >> gender >> age;
}
void exportData(char* outfileName){
ofstream outFile;
outFile.open(outfileName);
if(name == "emre"){
outFile << age << endl;
}
outFile.close();
}
int main(int argc, char** argv) {
if (argc != 3) {
cout << "Usage: ./project3 infile outfile" << endl ;
return 0;
}
importData(argv[1]);
exportData(argv[2]);
return 0;
}


A simple method would be to create a conditional loop where your program continues to read data until it reaches the end-of-file (eof) which I believe is a function that can be added to your program via a library. I'm still a beginner programmer by the way~

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Reading only first line from a txt file


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap