Solved

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

Posted on 2003-10-30
6
260 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 50 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

724 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