Thread: A Table Problem

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    22

    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.
    Last edited by kasun; 01-13-2004 at 06:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    22
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. problem with league table
    By vw1970 in forum C Programming
    Replies: 6
    Last Post: 01-04-2009, 09:16 AM
  3. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM
  4. HELP:Hasing Table
    By Amber_liam in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 04:25 AM
  5. Problem when data is printed.
    By Fuzzyman81 in forum C Programming
    Replies: 4
    Last Post: 11-06-2001, 03:24 PM