Thread: File I/O both ASCII and binary handling in C++

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    8

    File I/O both ASCII and binary handling in C++

    Well, like the title says, I've been looking for file I/O information (for both ASCII and binary), but have had little luck (Note: I'm quite new to C++ this just my third day reading tutorials [but I've learned a lot]). I need to find some more thorough information on the topic, any links or book references would be nice. I have been to many places, but they had limited information (esp. on binary handling).

    Many thanks, random.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, here's some quick code to get you started with binary file I/O:
    Code:
    //use iostream for standard console I/O
    #include <iostream>
    //use fstream for standard file I/O
    #include <fstream>
    
    int main()
    {
    	//declare a variable to write to the file
    	char ch='A';
    	
    	//open the file for binary reading and writing
    	std::fstream file("test.in",std::ios::in|std::ios::out|
    		std::ios::trunc|std::ios::binary);
    	//write the char to the file (note the cast and sizeof method)
    	file.write(reinterpret_cast<char*>(&ch),sizeof(ch));
    	//reset the 'read' or 'get' pointer to the beginning of the file
    	file.seekg(0,std::ios::beg);
    	//read from the file (again note the cast and sizeof)
    	file.read(reinterpret_cast<char*>(&ch),sizeof(ch));
    	//close the file
    	file.close();
    	
    	//output the character read from the file
    	std::cout<<ch<<std::endl;
    	//end the program
    	return 0;
    }
    as for text reading and writing, this site has a tutorial on that: http://www.cprogramming.com/tutorial/lesson10.html

    it's alot easier to find tutorials on text reading/writing - I didn't even know about binary reading/writing until college...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    One question: Why is it necessary to use reinterpret_cast? Is it because you didn't declare it the first time around?

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it enables you to put any data type out to the file, including your own home-baked ones... for example:
    Code:
    //use iostream for standard console I/O
    #include <iostream>
    //use fstream for standard file I/O
    #include <fstream>
    //use cctype for strlen
    #include <cctype>
    
    struct myStruct
    {
    	char*line;
    	int letters;
    };
    
    int main()
    {
    	//declare a variable to write to the file
    	myStruct A;
    
    	//fill in the struct with some dummy data
    	A.line="Hello World";
    	A.letters=strlen(A.line);
    	
    	//open the file for binary reading and writing
    	std::fstream file("test.in",std::ios::in|std::ios::out|
    		std::ios::trunc|std::ios::binary);
    	//write the char to the file (note the cast and sizeof method)
    	file.write(reinterpret_cast<char*>(&A),sizeof(A));
    	//reset the 'read' or 'get' pointer to the beginning of the file
    	file.seekg(0,std::ios::beg);
    	//read from the file (again note the cast and sizeof)
    	file.read(reinterpret_cast<char*>(&A),sizeof(A));
    	//close the file
    	file.close();
    	
    	//output the character read from the file
    	std::cout<<A.line<<std::endl<<A.letters<<std::endl;
    	//end the program
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Sorry to be a pest, but is there a way to check if a file is binary or ASCII?

  6. #6
    Banned
    Join Date
    Jun 2005
    Posts
    594
    technically every file is binary, but i believe there is a isascii()
    function which test to see if its in that range of characters,
    if there isnt which there might not be, you could always write
    your own.


    edit :
    yea i dotn think there was a isascii() function thought
    i saw it somewhere once, anyways you could write your
    own using a few of these maybe?
    Last edited by ILoveVectors; 08-15-2005 at 11:40 PM.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    open it manually and read it?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to Ascii Conversion
    By codemaster12 in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 10:57 AM
  2. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  3. Wininet Binary and ASCII
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 11-26-2005, 03:16 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Binary File I/O
    By sean in forum C Programming
    Replies: 7
    Last Post: 08-20-2004, 10:56 AM