C++ loading from txt files prob

This is a discussion on C++ loading from txt files prob within the C++ Programming forums, part of the General Programming Boards category; I wrote this code (pasted below) i'm a complete noob when i try to load it though it comes up ...

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

    C++ loading from txt files prob

    I wrote this code (pasted below) i'm a complete noob when i try to load it though it comes up with a bunch of funny characters in place of where it is supposed to show the value of bfile. I want this line here cout<< str <<"\n"; to show the value of variable no1 which has been saved into a text file earlier in the program. Instead it shows up with 3 asc11 symbols. Why is it doing this how do i fix it?

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    void main()
    
    {
    	char str;
    	int no1, no2;
    	cout<<"Please enter the first number: ";
    	cin>> no1;
    	cout<<"Please enter the second number: ";
    	cin>> no2;
    	cout<<"number 1 added to number 2 = "<< no1-no2 <<"\n";
    	ofstream a_file ( "add_calc.txt" );
    	a_file<< no1 ;
    	cout<< "numbers input were: ";
    	ifstream b_file ( "add_calc.txt" );
    	b_file>> str;
    	cout<< str <<"\n";
    	cin.get();
    	cin.get();
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    iostream.h is old and it's not used anymore. Please search the board.
    Also void main should be int main. Please first close file after writing to it in order to open it for reading.
    char is only 1 byte and you wrote integer (most likely 4 bytes to file). This is not what you want.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    char is only 1 byte and you wrote integer (most likely 4 bytes to file). This is not what you want.
    It will be converted to char, right?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    that's why you get funny characters: http://www.lookuptables.com/
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Look at this code:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main ( ) 
    {
        char test_ch;
        int test_int;
        cin >> test_int;
        ofstream ofile("test.txt");
        ofile << test_int;
        ofile.close( );
        ifstream infile("test.txt");
        infile >> test_ch;
        infile.close( );
        cout <<test_ch;
        
        return 0;
    }
    Here, you open file, write to file (since by default file is opened in textual mode) and close file. For example if you enter 12, number 12 will be placed in test_int and it will be written to file as 12 (characters 1 and 2). If you open that file for reading (default mode is textual) and read only one character, it will be 1, so test_ch will contain 1 since only one character is read from file.
    Does this make this issue clearer?
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    k thx ill try closing it. I have tried it with int as well but i didnt close it first. Also int is void. If i use the int command it gives me a whole lot of errors which i cant remember exactly what they are but its something to do with the improper use of the int command. Before this i always used void and it worked fine. So i switched to void and it works. Also using namespace std; throws up errors as well so i deleted that line.

    It could be because im using an old compiler? Im using microsoft developer studio version 4.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Yeah you're going to get a whole bunch of weirdness with such an old compiler.
    Try dev-c++, visual studio express or borland 5.5 - all pretty good and belong to this millenium, not the last one.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using c++ to edit txt files
    By trescenzi in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2006, 07:54 PM
  2. Loading multiple files
    By SanDiego619SDSU in forum C Programming
    Replies: 4
    Last Post: 10-29-2006, 01:37 AM
  3. txt files
    By Kitu in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2004, 03:55 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM
  5. Loading all files in a directory
    By steve_i83 in forum Windows Programming
    Replies: 2
    Last Post: 08-26-2001, 02:18 PM

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