Thread: basic i/o question cout<<str;

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    basic i/o question cout<<str;

    How can I modify the below text so that it couts all of example.txt?

    If you want to answer another question:

    1. How can I declare a string, for example
    int a=0;
    char a[256]=test; ((??))



    [code]

    #include <iomanip.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    #include <ctype.h>


    int main()

    {

    char str[10];

    //Used later

    ofstream a_file("example.txt");

    //Creates an instance of ofstream, and opens example.txt

    a_file<<"This text will now be inside of example.txt";

    //Outputs to example.txt through a_file

    a_file.close();

    //Closes up the file

    ifstream b_file("example.txt");

    //Opens for reading the file

    b_file>>str;

    //Reads one string from the file

    cout<<str;
    cout<<str;
    cout<<str;
    cout<<str;
    cout<<str;
    cout<<str;
    cout<<str;
    cout<<str;

    //Should output 'this'

    b_file.close();

    //Do not forget this!
    getch();

    }

    [c/ode]
    AIM: MarderIII

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    22
    Use getline.

    source.getline(placetooutputto,maxnumberofchars,en dingchar);

    so you might use b_file.getline(str,100,'/n');

    But of course you would want to put a /n at the end of the string. or any other character you choose. It will keep going until it finds that character. I'm not sure whether an error will come up or not if you get to the end of a file.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    If you mean displaying all of a file on cout:

    Code:
    #include <iostream>
    #include <fstream>
    
    int main (void) 
    {
    	std::ifstream in ("example.txt");
    	char buf[256];
    
    	while (!in.eof()) 
    	{
    		in.getline(buf,256,'\n');
    		std::cout << buf << std::endl;
    	}
    
    	return (0);
    }
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Now I have the below, which compiles, but crashes when i try to run it.

    Code:
    #include <iomanip.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    #include <ctype.h>
    
    
    int main()
    
    {
    
      char str[10];  
    
             //Used later
    
      ofstream a_file("example.txt");
    
    			//Creates an instance of ofstream, and opens example.txt
    
      a_file<<"This text will now be inside of example.txt/n";
    
    			//Outputs to example.txt through a_file
    
      a_file.close();
    
    			//Closes up the file
    
       ifstream b_file("example.txt");
    
    			//Opens for reading the file
    
     b_file.getline(str,100,'/n');
    
    
                 //Reads one string from the file
    
    
      cout<<str;
      cout<<str;
      cout<<str;
      cout<<str;
      cout<<str;
      cout<<str;
      cout<<str;
      cout<<str;
    
               //Should output 'this'
    
      b_file.close();
    
               //Do not forget this!
      getch();
    
    }
    AIM: MarderIII

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Because you declared str has being a 10 character array, but are trying to read 100 characters into it.

    Also, you're printing the same thing a bunch of times with all those couts, not reading from the file, if that's what you're trying to do...
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Nor does this work.


    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    #include <ctype.h>
    
    
    int main()
    
    {
      char str[100];
      ofstream a_file("example.txt");
      a_file<<"This text will now be inside of example.txt/n";
      a_file.close();
       ifstream b_file("example.txt");
     b_file.getline(str,100,'/n');
      b_file.close();
      getch();
    
    }
    AIM: MarderIII

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    The carriage return is "\n" not "/n".

    Also, does not work as in crashes, or does not work as in what you want it to do? The code I posted should work.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  8. #8
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    does not work as in freezes up and does not respond
    AIM: MarderIII

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This works fine on my computer :
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main()
    {
    
    	char str[100];
    	ofstream a_file("example.txt");
    	a_file<<"This text will now be inside of example.txt\n";
    	a_file.close();
    	ifstream b_file("example.txt");
    	b_file.getline(str,100,'\n');
    	cout << str << endl;
    	b_file.close();
    	getch();
    	return 0;
    }

  10. #10
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    ah, thanks
    AIM: MarderIII

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  3. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM