Solved

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

Posted on 2003-10-30
6
253 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

778 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