little help here please?

This is a discussion on little help here please? within the C++ Programming forums, part of the General Programming Boards category; hey im new to this board. anyway im writing this program that is supposed to allow a user to read ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    3

    Question little help here please?

    hey im new to this board. anyway im writing this program that is supposed to allow a user to read a text file full of records and display it on the screen. it is also supposed to allow a user to enter new records and have the record or records be appended to the file. now anotehr part of the program is the user can delete records. but you dont really delete them you just mark the as deleted so the progrma wont read them. here is a generic sample of what the file might look like:

    -----
    12345|John|Doe|724-213-4567|3215 Some Street|a city|a
    state|16057|0902|3.67|110 A|210 A|343 B|
    23456|Joe|Smith|724-213-4678|35 Another Street|a city|a
    state|16057|0902|3.04|110 B|210 B|343 B|
    *****
    +++++
    34567|Alan|Scott|412-213-4567|215 New Street|city B|a
    state|15221|0902|1.85|110 D|210 D|343 F|
    *****

    where the minuses start are records that are marked as deleted and where the stars are is where the deleted records end. where the plus signs are is where records that are added start and again where the stars are ends the added records.

    my question is how do i get the program to read this text file? someone told me to use the get function butbut im not qutie sure how i should invoke that or if that is the right way to go. any input by anyone would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Code:
    char buff[BUFSIZ];
    FILE *fp = fopen( "myfile.txt", "r" );
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        /* do stuff */
    }
    Those are the kinds of things you need to use to read each line of a text file.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Using character arrays:

    Code:
    #include <iostream>
    #include <fstream>
     
    ...
     
    char buffer[20];
    std::ifstream input("nameoffile.txt");
    ...
    // Get up to 19 characters or up to first '|' character from file
    input.get(buffer,sizeof(buffer),'|');
    Using strings:
    Code:
    #include <string>
    #include <fstream>
    #include <iostream>
     
    ...
     
    std::string data;
    std::ifstream input("nameoffile.txt");
    ...
    // Get as many characters as possible from file up to first '|'
    std::getline(input,data,'|');
    Of course all that should go in a loop of some kind and you'll likely want a struct or class of some kind to store a single "record" worth of data into and then an array or some other container to store all of the records from the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

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