473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading ´from textfile using getline

3
I just started to learn C++ and have some problem when I read a hole line from a textfile. I would be grateful if anyone can tell me what is wrong with my code.
I'm using Borland Developer Studio 2006.


//------------------------------------------------------------------------------
// getFile
//------------------------------------------------------------------------------
// Read from a textfile
// Input: nameList[] (string) , FILENAME (string)
// output: countName (int)
//------------------------------------------------------------------------------

int getFile(vector<string> &nameList,const string FILENAME)
{

int countName = 0;

fstream nameFile;

nameFile.open(FILENAME.c_str(), ios::in);

//Read line by line from file to nameList until EndOfFile

while (getline(nameFile,nameList[countName]))
{
countName++;
}

nameFile.close();



return countName;
}
Jan 18 '08 #1
3 2877
weaknessforcats
9,208 Expert Mod 8TB
What happens when you run this code?
Jan 20 '08 #2
Arn
3
What happens when you run this code?
It runs to the line "while (getline(nameFile,nameList[countName])) ".
On this line nothing happens.
Jan 21 '08 #3
Arn
3
It runs to the line "while (getline(nameFile,nameList[countName])) ".
On this line nothing happens.
I just solved the problem, it seems that I coudn't get the line directly to my vector. I had to read to a string and after that puch_back to my vector.

Heres my new code. If anyone got another way to do it so please show it.

// getFile
//------------------------------------------------------------------------------
// Read from a textfile
// Input: nameList[] (string) , FILENAME (string) , newcountName (int)
// output: newcountName (int)
//------------------------------------------------------------------------------

int getFile(vector<string>& nameList,const string FILENAME,int newcountName)
{

fstream nameFile;
nameFile.open(FILENAME.c_str(), ios::in);
string nameToGet;


while (getline(nameFile,nameToGet))
{
nameList.push_back(nameToGet);

newcountName++;

}

nameFile.close();

return newcountName;
}
Jan 21 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
2
by: BjoernJackschina | last post by:
Hello, many thanks for the solution of my last problem but I think I will use my own simple solution. I want to find anagrams. The file "d.txt" looks like this: opts stop fnu fun opts ...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
8
by: nick | last post by:
Hi all can any one please tell me what is wrong in this code?? I'm new to deal with text files and extract data. i'm trying to look for data in a text file (3~4 pages) some lines start with a...
2
by: novacreatura | last post by:
Hi, I have a project that's supposed to create a program for a "Dating Service". The first part of the program is to read a textfile of profiles which include names, age, etc...into a string...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
1
by: Wx | last post by:
Hello. I'm trying to read a textfile written by the NTBackup utility on Windows 2003 SBS. The problem is that when i print the output, it looks like this: S t a t o : b a c k u p O p e r a...
3
by: newguy194 | last post by:
I have written the skeleton for a program which is supposed to dissect a text file by first getting the entire contents of the textfile as a string, then searching that string for a matching first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.