general C++ help - compilers, file access etc

This is a discussion on general C++ help - compilers, file access etc within the C++ Programming forums, part of the General Programming Boards category; hi there, i'm pretty new to C++ and currently my programming is very much a crude mixture of C with ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    general C++ help - compilers, file access etc

    hi there,

    i'm pretty new to C++ and currently my programming is very much a crude mixture of C with some C++ elements as i'm still trying to get my head around object orientated programming. Firstly i was wondering if anyone had any suggestions of which compiler to use...

    I have been using mainly the Borland C++ V4.51 IDE for my compiling as i had previously used it for C coding, but i notice it doesn't want to let my use the namespacing features of C++ - does anyone know why this is!?? I presume it could be that its compliant with an earlier standard of C++ as borland is now 10 years old!

    So, I have also tried (and failed!) the Visual Studio .NET 2003 but thats pretty confusing and doesn't seem to let me use some of the standard include files... does anyone know of a good tutorial? the help isn't very helpful i find.

    anyway, this brings me to a programming question about file handling.

    Code:
    char ch;
    ifstream infile("infile.txt");
    ofstream outfile("outfile.txt");
    
    	while(!infile.eof())
    	{
    		infile.get(ch);
    		outfile << ch;
    
    	}
    if say, i have this snippit in my program how do i reset the file pointer to the begenning (or end) of the file so i can read it again? - i want to scan the entire file for the last entry as i need to use that before i use the rest of the data.

    i tried using the fseek function but thats a C, not a C++ function isn't it?

    Appologies for the lame questions, but any help will be thankfully received.

    Dave

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Also, your code at the last iteration of the loop will read EOF and write that to your output file--not desirable. You want to modify your loop to become like the following.

    Code:
    while(infile.get(ch))
    { 
        outfile << ch;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    34
    I might be mistaken, but I think you call infile.clear() if you want to clear the "end of file" flag and read it again...maybe infile.seekg(0, ios::beg) to seek to the top again.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Quote Originally Posted by veecee
    I might be mistaken, but I think you call infile.clear() if you want to clear the "end of file" flag and read it again...maybe infile.seekg(0, ios::beg) to seek to the top again.

    You are correct here.


    As for the fseek, yea it is a C function but there is absolutly nothing wrong with using File pointers and C functions in a C++ program. the file rotuines are defined in iostream anyway.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    2
    cool, cheers gys,

    got the file i/o sorted thanks.. i knew it'd be a simple procedure, just couldn't get it to work! Thanks for the file reading loop also... much more straight forward than what i was doing...

    dave

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> the Visual Studio .NET 2003 but thats pretty confusing and doesn't seem to let me use some of the standard include files...

    It should. If you tried <fstream.h>, that won't work, because that is not standard. If you tried <fstream>, then that should work because it is standard and VC++ 2003 is very standards compliant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

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