?
Solved

reading from a text file one line at a time...

Posted on 2003-10-30
6
Medium Priority
?
265 Views
Last Modified: 2010-04-01
hi...

I need to read from a file :(presumed as strings..)
e.g:

20031030154545
20031030164545
....
....

get each line and manipulate it and print the output as say(say decrement by 3 hrs it..)
20031030124545
20031030134545
...
what i am basically doing is pass each line to a function that converts the string in to a Date object ..and print it..

I am getting some garbage values...

Any help would be appreciated...regards

code:

char buffer[14];
ifstream file("filename.txt");
while(!file.eof()){

file.getline(filename,14);
//strToDate converts the string int to date object...
Date date = strToDate(buffer);
...
}
0
Comment
Question by:chandra1977
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9654131
You have a value called buffer but you read into a value called filename. I think the getline should read:

file.getline(buffer, 14);

Also, you want to delete the other copy of this question that got posted. You can look in the CS (community support) section for directions.

-bcl
0
 

Author Comment

by:chandra1977
ID: 9655215
please delete the question  i have posted..i haved mistakenly posted it twice...

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20783484.html#9654131
regards,

sai
0
 

Author Comment

by:chandra1977
ID: 9655218
bcl,

Thats not the solution i was looking for...i have mistyped the variable...hoped u had a better soln to offer..cheers
0
 
LVL 11

Accepted Solution

by:
bcladd earned 150 total points
ID: 9656845
Okay, how to post a question that can get an answer:

More information on where you are getting garbage (have you printed out what you get from the getline? What do you get?) would help answer the question.

I looked at what you have and you don't leave enough room for the \0 at the end of the character buffer. If you move the number up to 15 you should be okay.

I would urge you to consider using the std::string library because it would be easier to avoid worrying about size of the strings.

Hope this helps, -bcl
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546327
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bcladd {http:#9656845}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question