Solved

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

Posted on 2003-10-30
6
247 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now