473,568 Members | 2,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading ´from textfile using getline

3 New Member
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,co nst string FILENAME)
{

int countName = 0;

fstream nameFile;

nameFile.open(F ILENAME.c_str() , ios::in);

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

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

nameFile.close( );



return countName;
}
Jan 18 '08 #1
3 2892
weaknessforcats
9,208 Recognized Expert Moderator Expert
What happens when you run this code?
Jan 20 '08 #2
Arn
3 New Member
What happens when you run this code?
It runs to the line "while (getline(nameFi le,nameList[countName])) ".
On this line nothing happens.
Jan 21 '08 #3
Arn
3 New Member
It runs to the line "while (getline(nameFi le,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(F ILENAME.c_str() , ios::in);
string nameToGet;


while (getline(nameFi le,nameToGet))
{
nameList.push_b ack(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
7038
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 file... Thank you to all of you who can help me with this one...
3
2193
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
1309
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 post opts tops emos some So I compare every word order but in my programm I can only read the
19
10283
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly...
8
6045
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 word "red" first if find(red) then print the last 5 letters of that string and if red is not found at the begining of the string then do nothing and...
2
3019
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 array, and be able to add,edit,remove to the textfile of profiles during runtime. What would be the most efficient way to do this to make it easiest as...
1
1419
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 humanresources\compensation files from tcinfotest to tcinfo -----------------------
1
3197
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 z i o n e : b a c k u p D e s t i n a z i o n e b a c k u p a t t i v o : F i l e N o m e s u p p o r t o : " l u m e v e . b k f c r...
3
1628
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 word, then skipping over a delimiter and returning the correct word. For some reason it does not return anything, and I do not see where my error is...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.