working with a .dat file

This is a discussion on working with a .dat file within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a .dat file that i have to read from, and i need to be able to add ...

  1. #1
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33

    Question working with a .dat file

    Hey,

    I have a .dat file that i have to read from, and i need to be able to add a whole column in the .dat file together and then divide it by 20 and and print the answer.

    does anyone know how I would go about doing that? I'm literally going mad cause I cannot find a way to do it, as i have never worked with .dat files,

    i'm not asking for code just an idea of how I have to do it cause it's for my coursework,

    thanks xXx
    Microsoft: "You have questions, we having dancing paperclips"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Treat the .dat file as a normal (text?) file with whatever is the specified format.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    sorry i'm still confused on what you mean?
    Microsoft: "You have questions, we having dancing paperclips"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    Suppose you have a .txt file that you have to read from, and which you need to be able to add a whole column in the .txt file together and then divide it by 20 and and print the answer.

    How would you do it?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Read up about fstream library. It works with file IO
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    well so far I have done this, it can read the file

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <assert.h>
    
    
    
    void main( int argc, char* argv[] )
    {
       assert( argc >= 2 ) ;
       ifstream accfile( argv[1] ) ;
       if ( !accfile )
       {
          cerr << "File " << argv[1] << " not found\n" ;
          exit(1) ;
       }
    
    
    }
    
    Contestants c ; 
    accfile >> c.name >> c.club >> c.age >> c.500 >> c.1000 >> c.1500 >> c.2000 ;
    
    const int ACC_MAX = 20;
    
    Contestants accs [ACC_MAX];
    int filesRead = 0;
    
    do
    {
       accfile >> accs[filesRead].name
               >> accs[filesRead].club
               >> accs[filesRead].age
               >> accs[filesRead].500
               >> accs[filesRead].1000
               >> accs[filesRead].1500
               >> accs[filesRead].2000 ;
      if (accfile)
      {
        filesRead++ ;
      }
    
      } while (accfile) ;
    
      cout << filesRead << "Accounts Read\n" ;
      
    }
    and i've have been working on a struct, it isn't complete yet but this is what i have so far

    Code:
    
    struct Contestants
    {
    char  name;
    char club;
    int age;
    double 500;
    double 1000;
    double 1500;
    double 2000;
    };
    
    void raceWinner ( Contestants c )
    {
      for ( c.2000 < ACC_MAX; 
    
    }
    i could be very well barking up the wrong tree, but i am trying everything at the moment.
    Microsoft: "You have questions, we having dancing paperclips"

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Code:
    struct Contestants
    {
    char  name;
    char club;
    int age;
    double 500;
    double 1000;
    double 1500;
    double 2000;
    };
    That doesn't compile, does it?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    I havent tried yet, I'm just playing around with what I might need to do to try and get the program to read all the numbers in 500 add them all together and divide it by 20 and print the answer
    Microsoft: "You have questions, we having dancing paperclips"

  9. #9
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    I've been trying to google my problem for like 3 days now, and this forum was my last resort cause I wanted to try and figure it out, but im at the end of my teather at the moment
    Microsoft: "You have questions, we having dancing paperclips"

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Variable names cannot start with numbers.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    ok, thanks, ive changed them now
    Microsoft: "You have questions, we having dancing paperclips"

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, does your file have multiple columns and you only need one. If so, you don't need the struct at all. Just read the other columns and discard the values, because you don't need them. This task is so basic, that one can hardly imagine you might be required to use a struct.

    By the way, code goes into a function.

    It's really something as simple as
    Code:
    while (fileIn >> temp >> rubbish >> important >> trash >> throwaway) {
        //add important to an accumulator variable
        //increment line counter if you need to know it
    }
    //divide accumulator value by 20 (or by how many lines there was)

  13. #13
    c++ beginner so be nice! karlawarla's Avatar
    Join Date
    Nov 2006
    Location
    West London, UK
    Posts
    33
    ok thanks for the reply,

    I knew it would probably be a simple answer, but I''m very new to c++ and I'm not very good at it at the moment,

    Thanks again Karla x
    Microsoft: "You have questions, we having dancing paperclips"

  14. #14
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    void main() is evil. Check Salem's avatar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21