Format data from a file

Posted on 2004-05-02
Last Modified: 2010-04-01
In my code, I read data from a are the first 2 lines...


I stored each line and what I want to do is put it in this format:

Basically, what I want to do is scan through the buffer (where I stored the line), and put them in that format.

How would I do this?
Question by:jewee
  • 4
  • 4

Expert Comment

ID: 10974790
Hi jewee,

char dd[3], mm[3], yy[3] ;

scanf ("%2s%2s%2s", yy, dd, mm) ;
printf ("%s/%s/%s", dd, mm, yy) ;

or sscanf() and sprintf() if you want to manipulate strings instead of input output.


Author Comment

ID: 10974838
When I read in data to a file, that data is at the end, so I read it into a char buffer array.  Should I just set, for example, the first 2 bytes which is 99 into yy...etc??

Author Comment

ID: 10974845
or use a pointer and scan through the buffer?

sample code?
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.


Expert Comment

ID: 10974950
I don't understad what you really want to do now. Do you have a file containing lines "990102", "010901" etc., or you have some data and inside are these dates? Well, let's pass this and suppose that you have allready read a line that contains your date.

If you have a string containing "990102" in the middle it still depends whether the position is fixed or it is prefixed with a word (possibly "date:") or the position of the date is fixed (like 3rd word from beginning).

but if you know the exact position of the date in the string (lets say 'a') you can:
separate the date copying the substring out: strncpy (szDate, &szLine[a], 6) ;
and sscanf() it
or you can separate it using numbers
    int dd, mm, yy ;
    scanf ("%2d%2d%2d, &yy, &dd, &mm)

or you can get the numbers by your own:
    yy = (szLine[a]-'0')*10 + (szLine[a+1]-'0') ;
    dd = (szLine[a+2]-'0')*10 + (szLine[a+3]-'0') ;
    mm = (szLine[a+4]-'0')*10 + (szLine[a+5]-'0') ;

there are numerous approaches, you have to decide what to do according to your specific situation (for example if you want to calculate with the date further it would be better to get numbers instead of substrings).


Author Comment

ID: 10974986
I know exactly where the string is within the line...this is how I extract it...

std::string last_field = line.substr(42);

Author Comment

ID: 10974995
I'm confused with how I use sscanf to separate it considering the location of the year, month and day needs to be changed to month, year, and day.

Accepted Solution

skypalae earned 500 total points
ID: 10975013
so you answered you question perfectly by ourself :)

std:: string yy = last_field.substr(0, 2) ;
std:: string dd = last_field.substr(2, 2) ;
std:: string mm = last_field.substr(4, 2) ;

cout << dd << "/" << mm << '/' << yy ;


Expert Comment

ID: 10975019
And don't get confused by scanf and printf. These are C (not C++) functions and if you don't understand them don't bother. You can learn C later.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

815 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

7 Experts available now in Live!

Get 1:1 Help Now