Files and record handling in Dev C++

Posted on 2010-01-02
1 Ratings
Last Modified: 2013-11-17
I need information about text files handling (create, edit, delete and remove) records in Dev C++
Sources of info with examples will be appreciatted.
Question by:voipccs
    LVL 33

    Accepted Solution

    It does not matter Dev C++ or another C++ compiler.
    Check here and you will find everything with examples that will work for you:

    fopen, fread, fwrite, fclose, fprintf, fscanf, ... .

    FILE fin = fopen( "text.txt", "r");
    char text[100];
    fread(text, sizeof text, 1, fin);

    Better way:
    You will find examples with the text files.

    int main()
            char str[2000];
            fstream file_op("c:\\test_file.txt",ios::in);
            while(file_op >> str)
            cout << str ;
            return 0;

    Rename file:

    LVL 33

    Assisted Solution

    If I understand the question, you can read the text into a vector (or an array/container), each line as a separate element. It will allow you to handle each record separately. In the end you can just re-create the file and save all records in it.
    Firstly, you need to the basic file I/O.
    LVL 9

    Assisted Solution

    LVL 39

    Assisted Solution

    >> I need information about text files handling (create, edit, delete and remove) records in Dev C++

    I see pgnatyuk has provided you with information about the standard C IO libraries, I'll compliment that with some info about the standard C++ file stream libraries.

    create: You create/write files using ofstream and read them using ifstream

    edit: The way you edit is you read data into memory, modify it and write it back to the stream. If the new data is the same size as the old this is a simple case of rewinding the file pointer and writing. If the data is smaller or larger you'll have to implement a strategy to take care of this. In the case of this being a simple text file and you are modifying the file as a whole you can just overwrite in the case of the data being larger, of truncate (by closing the stream and re-opening with the trunc flag) and re-write in the case of the new data being smaller. If you are dealing with a single record in a collection of records it's more complex and, probably, beyond the scope of this one question (although we can discuss this further if you wish).

    delete: If you refer to the file as a whole standard C++ provides no way to do this; however, there are standard Posix functions you can use as pgnatyuk has already explained. If you mean a record in a collection of records, again the strategy depends upon your data format and, probably, beyond the scope of this one question (although we can discuss this further if you wish).

    remove: I'm not sure that I understand the semantic difference between remove and delete, maybe you could clarify this for us?
    LVL 7

    Assisted Solution

    std::ofstream textfile("text.txt");

    opens the textfile in the current directory fro write. if not exists it creates a new file. if exists it truncates the file.
    std::ofstream textfile("text.txt", std::ios_base::app | std::ios_base::out);

    opens for append to existing textfile. creates new file if not exists.

    deletes file text.txt if exists and was not open by a program.

    would suggest to always write new text file after edit as record length may be different for each text line and text files were stored as streams with linefeed chars (at windows with carraige-return + linefeed chars) for line end.


    Author Closing Comment

    I appreciatte your prompt reply I will proceed to review all the info and keep you posted as soon as issues arise.
    Thanks for all!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    737 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

    15 Experts available now in Live!

    Get 1:1 Help Now