File I/O Question

This is a discussion on File I/O Question within the C++ Programming forums, part of the General Programming Boards category; I've read the tutorial on this site, but I am not quite sure what is means. Ex. Say if I ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Question File I/O Question

    I've read the tutorial on this site, but I am not quite sure what is means. Ex. Say if I make a Readme type file or help file in notepad, could I implement that into my program. Is that what file i/o is used for? Not sure what it means.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It means like instead of printing to cout (stdout, the screen) you can print to a file; and instead of using stdin (cin) you can read from a file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    OH, ok. I was way off. Thanks for the reply.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    think of it this way: how does a store save information about their sales for the day when they shut the computers off? how does a game know where you left off when you exit?

    basically, you can write information to a file to save it for later, or you can give it a file to parse data from, or a whole bunch of things... sure stores use databases, but it's (very) vaguely the same idea...

    for example:
    Code:
    #include<iostream>
    #include<fstream>
    
    int main()
    {
    	int num;
    	
    	std::cout<<"What do you want to do:\n"
    		<<" 1. Save a number\n"
    		<<" 2. Retrieve the number\n";
    	
    	std::cin>>num;
    	std::cin.ignore(1);
    
    	if(num==1)
    	{
    		std::cout<<"Enter the number: ";
    		std::cin>>num;
    		std::cin.ignore(1);
    		
    		std::ofstream file("test.in",std::ios::trunc);
    		file<<num;
    		file.close();
    	}
    	else if(num==2)
    	{
    		std::ifstream file("test.in");
    		
    		if(!file)
    		{
    			std::cerr<<"File not available, exiting\n";
    			exit(1);
    		}
    		
    		file>>num;
    		file.close();
    		std::cout<<"The last number was: "<<num<<std::endl;
    	}
    	else
    	{
    		std::cerr<<"Invalid option, exiting\n";
    		exit(2);
    	}
    }
    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
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    [qoute]
    I've read the tutorial on this site, but I am not quite sure what is means. Ex. Say if I make a Readme type file or help file in notepad, could I implement that into my program. Is that what file i/o is used for? Not sure what it means.
    [/qoute]
    Yes you can; in the sense that mutiple programs with the same 'readme' content can print it on to the screen. reducing the overall size.
    but this is only a very small advantage:
    (read the previous post; by major_small);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 10:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 03:15 PM

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