-
A Table Problem
I have a problem with a data table please help.
Problem: I have this 2 .dat files called LFILE & RFILE. LFILE contains some names in an M number of rows.
RFILE contains numerical values that match the names in LFILE, also in M rows. I am to write a function to combine these 2 file’s data and write a new file called LRFILE to the disk. LRFILE must present the above names and matching values in a clean table (height -M rows). I wrote this code but it doesn’t produce the correct result. Help!
Code:
//**********
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void main()
{
char* f1name = "LFILE.dat";
char* f2name = "RFILE.dat";
char* f3name = "LRFILE.dat";
ifstream tfile1(f1name, std::ios::in );
ifstream tfile2(f2name, std::ios::in );
ofstream ofile(f3name, std::ios::out);
vector <string> f1dat;
vector <string> f2dat;
char f1str[100];
char f2str[100];
// read
while(!tfile1.eof()){
tfile1.getline(f1str,50, '-');
tfile2.getline(f2str,10, '.');
ofile.write(f1str, 50);
ofile.write(f2str, 10);
}
}
//*************
example files-----
================================
LFILE.dat
John Williams (USA)-
Darrell Pace (USA)-
Tomi Poikolainen (Finland)-
Darrell Pace (USA)-
Jay Barrs (USA)Sébastien Flute (France)-
Justin Huish (USA)-
Simon Fairweather (Australia)-
Doreen Wilber (USA)-
Luann Ryon (USA)-
Keto Lossaberidze (USSR)-
Seo Hyang-soon (South Korea)-
Kim Soo-nyung (South Korea)-
Cho Youn-jeong (South Korea)-
Kim Kyung-wook (South Korea)-
Yun Mi-Jin (South Korea)-
=================================
RFILE.dat
1972.
1976.
1980.
1984.
1988.
1992.
1996.
2000.
1992.
1996.
2000.
1972.
1976.
1980.
1984.
============================
These 2 files must combined into a single file as a table.
-
Consider that the first line of the file looks like this, if you stored the whole line in an array
char line[] = "John Williams (USA)-\n";
Given say tfile1.getline(f1str,50, '-');
You have to figure out if the '-' left in the file, or is it read in from the file and stored in f1str?
Whatever it does, you're going to have to do something with the - and newline characters.
> tfile1.getline(f1str,50, '-');
Also, you should say
tfile1.getline(f1str,sizeof(f1str), '-');
Lying to routines about the size of buffers is asking for trouble.
> ofile.write(f2str, 10);
You're writing the whole array, not just the part which contains useful data read from the file.
Additionally, you'll want to output a newline
-
Think that the ‘-‘ character isn’t a matter. Now my problem is that, as you said, I’m writing more than the useful part of the arrays. So some invalid chars also gonna be written to fill up the rest of the string. I can’t find a way to filter out the valid part of the array and write it to the file. Also, how can I write both the name and the year in a single row? Because when I call write() twice for the name and the year, they are printed in 2 lines.
**Thanks for your interest in this.