Dealing with files thru FILE *

This is a discussion on Dealing with files thru FILE * within the C++ Programming forums, part of the General Programming Boards category; After doing some reading on FILE data type I decided to practice, I came up with this little program that ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Dealing with files thru FILE *

    After doing some reading on FILE data type I decided to practice, I came up with this little program that reads any part of any file:

    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
        FILE * file;
        char * file_name = new char[255];
        int read_from, lenght, file_size;
        char * output;
    
        cout << "What file do you want to be read? ";
        cin >> file_name;
    
        if(!(file = fopen(file_name, "r+")))
        {
            cout << "Could not open file.";
            return 0;
        }
    
        fseek(file, 0, SEEK_END);
        file_size = ftell(file);
        rewind(file);
    
        cout << "File " << file_name << "(" << file_size << "bytes) sucessfully opened.\n\n";
    
        cout << "Read from offset: ";
        cin >> read_from;
    
        fseek(file, read_from, SEEK_SET);
    
        if(read_from > file_size)
        {
            cout << "Given file does not reach that far!\n";
            return 0;
        }
    
        cout << "How many bytes to be read? ";
        cin >> lenght;
    
        if((read_from + lenght) > file_size)
        {
            cout << "Given file does not reach that far!\n";
            return 0;
        }
    
        output = new char[lenght];
    
        for(int x = 0; x < lenght; x++)
        {
            output[x] = getc(file);
        }
    
        cout << "Output: " << output;
        return 0;
    }
    I'd like some suggestions, critics, tips on how to make my code better.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    First, decide whether you want to program in C or C++.

    > output = new char[lenght];
    You also need to
    - allocate space for a \0
    - actually append a \0 to make it a proper string

    And for both new calls, a corresponding delete call.

    Not to mention closing the file when you're done.
    This includes all your error paths as well (close file, memory cleanup)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    First, decide whether you want to program in C or C++.
    You mean, using FILE data type instead of fstream class?

    > output = new char[lenght];
    You also need to
    - allocate space for a \0
    - actually append a \0 to make it a proper string
    So instead of "new char[lenght]", I would have to use something like "new char[lenght + 1]", since '\0' takes 1 byte, correct?

    And for both new calls, a corresponding delete call.
    Doesn't the program free memory after it's done?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Quote Originally Posted by dhuan
    Doesn't the program free memory after it's done?
    No. A modern operating system would likely release all the memory allocated to the process, but relying on this leads to sloppy programming technique.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    One improvement would be to spell "length" properly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You are also prone to buffer overruns because of your C-style strings (never use cin >> with C-style strings). Suggest you switch to std::string and std::getline instead of std::cin >>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing with nonexistent file input
    By dlwjiang in forum C Programming
    Replies: 2
    Last Post: 06-24-2010, 04:03 AM
  2. Connecting and dealing with source files
    By alanb in forum C Programming
    Replies: 19
    Last Post: 08-28-2009, 11:12 AM
  3. stack smashing dealing with files...
    By smoking81 in forum C Programming
    Replies: 13
    Last Post: 03-27-2008, 12:18 PM
  4. Dealing with windows files
    By Hexxx in forum C++ Programming
    Replies: 7
    Last Post: 08-17-2006, 02:11 PM
  5. dealing with files.....
    By agerealm in forum C Programming
    Replies: 2
    Last Post: 02-02-2002, 12:31 PM

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