Thread: outputting a variable into a text file

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    outputting a variable into a text file

    hello all, i have been doing some C++ tutorials and am learning to work with txt files and reading and writing to them. my problem lies in that is it possible to print variables to a txt file? here is the code for a program i am working on
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <conio.h> // for getch()
    #include <fstream> // for ofstream
    using namespace std;
    
    int main()
    { 
         ofstream a_file( "numbers.txt" );
        int x;
    cout<<"this is a test of my knowledge of using fstream";
     for (  x = 0; x < 100; x++); // make x inrement up to 100
    
      a_file << x; // only prints the actual x not the variable??
    a_file.close(); // closes file
    
     
    getch(); // wait for keypress
    };
    as you could probably gather, the program runs fine but in the txt file it only shows a bunch of numbers, i was wondering is there a special way i can make it print the variable X? or is it even possible? thank you in advance for all the help

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    That program should print the value of x, 0 to 99 without anything inbetween.
    Code:
    012345678910111213141511617181920...
    Isn't that what you wanted to happen?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    for (  x = 0; x < 100; x++); // That semicolon ends the for loop statement
        a_file << x;  // Which means this prints the last value of x after all the looping
    This code should print "100" to numbers.txt and that's it. Now, if it does indeed print out a bunch of numbers as you say, then I'll assume that isn't the exact code, which leaves me with the question... why aren't you pasting the exact code?

    ... and on the subject of "why"-- Why are you using a non-standard library like conio.h for a function to pause your program when you can just use cin.get() from <iostream>? Also <cstdlib> isn't be used anywhere in there.

    Now as for your actual question... could you be more specific than "special"? I think printing the value of x is pretty special, so I don't know what you want it to do. Let me take a wild guess and say you wanting your program to output something like the following
    Code:
    x = 0
    x = 1
    x = 2
    ...
    Last edited by SlyMaelstrom; 08-30-2006 at 07:39 PM.
    Sent from my iPadŽ

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't need a semicolon after the closing brace . . . functions aren't the same as classes and structures.

    If you did indeed want the output Sly described, try this:
    Code:
    a_file << "x = " << x << endl;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM