IO operations help

This is a discussion on IO operations help within the C++ Programming forums, part of the General Programming Boards category; Hello, Today i am playing with IO's, and here is the example that i want to work out: Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    172

    IO operations help

    Hello,

    Today i am playing with IO's, and here is the example that i want to work out:


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    
    void openFile(std::fstream &out, std::string fileName, int x){
    
    
        switch (x){
    		case 'rt':
    		out.open (fileName.c_str(),std::ios::in);
    		break;
    		case 'wt':
    		out.open (fileName.c_str(),std::ios::out);
    		break;
    		case 'at':
    		out.open (fileName.c_str(),std::ios::app);
    		break;
    		case 'rb':
    		out.open (fileName.c_str(),std::ios::in | std::ios::binary);
    		break;
    		case 'wb':
    		out.open (fileName.c_str(),std::ios::out | std::ios::binary);
    		break;
    	}
    
    	if ( !out.is_open()){
    		std::cout << "Cannot open file: in_file! Line:" << __LINE__<< std::endl;
    		exit(EXIT_FAILURE);
    	}
    }
    
    int main(int argc, char **argv){
    
    char buffer[100]; 
    std::fstream out_file;
    std::fstream in_file;
    
    
    openFile(out_file,"example.txt",'wt');  // WORKS
    out_file << "Radi-";
    out_file.close();
    out_file.clear();
    
    openFile(in_file,"example.txt",'rt');  // WORKS
    in_file.read(buffer, 5); 
    std::cout << buffer; 
    in_file.close();
    in_file.clear();
    
    openFile(out_file,"example.txt",'at');  // DOES NOT APPEND
    out_file << "Radio";
    out_file.close();
    out_file.clear();
    	
    openFile(in_file,"example.txt",'rt'); //  WORKS but prints out this strange character
    in_file.read(buffer, 10); 
    std::cout << buffer; 
    in_file.close();
    in_file.clear();
    
    openFile(out_file,"example.bin",'wb'); // not binary - correction
    out_file << "Radik\n";
    out_file.close();
    out_file.clear();
    
    openFile(out_file,"example.bin",'rb'); // not binary - correction
    out_file.read(buffer, 5); 
    std::cout << buffer; 
    out_file.close();
    out_file.clear();
    
    return 0;
    
    }

    so i am opening and closing the files and reading and writing to them. the problems are stated in the code. Can someone explainwhat is going on..

    output: Radi-Radi-Radik
    Last edited by baxy; 11-29-2012 at 04:59 AM. Reason: - correction

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    172
    ok i learned how to use read and write

    Code:
    int main(int argc, char **argv){
    
    char buffer[100]; 
    std::fstream out_file;
    std::fstream in_file;
    int a[10];
    
    for (int i = 0 ; i<10 ;i++)
      a[i] = i;
    
    openFile(out_file,"example.txt",'wt');
    out_file.write("RADI",4);
    out_file.close();
    out_file.clear();
    
    openFile(in_file,"example.txt",'rt');
    in_file.read(buffer, 4); 
    std::cout << buffer; 
    in_file.close();
    in_file.clear();
    
    openFile(out_file,"example.txt",'at');
    out_file.write("RADI-------",10);
    out_file.close();
    out_file.clear();
    	
    openFile(in_file,"example.txt",'rt');
    in_file.read(buffer, 10); 
    std::cout << buffer; 
    in_file.close();
    in_file.clear();
    
    openFile(out_file,"example.bin",'wb');
    out_file.write((char *)&a , (sizeof(int)*10));
    out_file.close();
    out_file.clear();
    int v[10];
    openFile(out_file,"example.bin",'rb');
    out_file.read((char *)&v, (sizeof(int)*10)); 
    for(int i =0; i<10;i++)
    std::cout <<v[i] <<"\n"; 
    out_file.close();
    out_file.clear();
    
    return 0;
    
    }
    
    but still i get some strange characters when i print out my strings...
    
    RADI�RADI�0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit operations?
    By yu_raider in forum C Programming
    Replies: 5
    Last Post: 03-17-2009, 11:31 AM
  2. operations
    By moussa in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 04:12 PM
  3. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  4. bit operations
    By agarwaga in forum C Programming
    Replies: 3
    Last Post: 05-18-2006, 11:43 AM
  5. what do these operations do?
    By the_head in forum C Programming
    Replies: 4
    Last Post: 01-29-2005, 01:45 PM

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