• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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

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
chandra1977
Asked:
chandra1977
  • 2
  • 2
1 Solution
 
bcladdCommented:
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
 
chandra1977Author Commented:
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
 
chandra1977Author Commented:
bcl,

Thats not the solution i was looking for...i have mistyped the variable...hoped u had a better soln to offer..cheers
0
 
bcladdCommented:
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
 
tinchosCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now