FAQ ~ CS1440-101 and 103 , Rahman Tashakkori's Classes

Frequently Asked Questions
Click on question summary to jump to complete questions and answers.

1.  Compiler error question
2.  Reading data from a file
3.  Writing output to a file
4.  Changing format of numbers to display 2 decimal points
5.  Difference between get(c) and cin >> c
6.  Difference between put(c) and cout << c
7.  
8.
9.
10.

1.  Compiler Error Question
Question:
I get the following error whenever I compile my program, what could be the reason?

collect2: ld returned 1 exit status
/bin/ld:
Object file format error in: prog2: read_cur_obj_info: bad file magic number(0x6923)

 

Answer:
It is very likely that you have forgotten the extension at the end of your file.  If that is a C++ file, then you have to use a .C extension.  In the above example, your file must be named prog2.C to compile correctly.

Back to Top

 
2.  Reading data from a file
Question:
How can I read data from a file into my program?

 

Answer:
One way to read data from a file is shown in the code below.  It shows how to declare an input file stream, open a file, and read all the data from the file.

1.  Place the following include directives in your program file:
     #include <fstream.h>
     #include <stdlib.h>

2.  Declare an ifstream variable in your main program
  
   ifstream in;

3.  Open the input file:  

     in.open("input.dat"); 
     //where input.dat is the name of output file 

     if (in.fail()) // in.fail() returns true if file won't open     
     {       
        cout << "Input file opening failed.\n";      
        exit(1);  // if the file did not open, program ends 
     }

4.  Use the stream in to read input to the file input.dat just like you would use cin, and it will get input from the file just like cin gets input from the user:

     in >> num;  // writes to file

5.  Don't forget to close the file!
     in.close();  // close input file       

Back to Top
3.  Writing output to a file
Question:
How can I write output to a file?

 

Answer:
There is more than one way to do this.  The simple, lazy (although not the best) way to write output to a file is to run a program that prints normally to the screen using cout statements, and redirect the output using the > operator at the cs prompt.
% a.out > output.txt

In order for this to work, there cannot be another file named output.txt in the directory.  This doesn't do anything great; it prints cout statements to a file rather than to the screen.  For most programs, you would not want to use this method.  If there are any cin statements in your program, you would never see any cout statements to prompt you for input, which makes this method undesirable.

1.  Place the following include directives in your program file:
     #include <fstream.h>
     #include <stdlib.h>

2.  Declare an ofstream variable in your main program
  
   ofstream out;

3.  Open the output file:  

     out.open("output.dat"); 
     //where output.dat is the name of output file 

     if (out.fail()) // out.fail() returns true if file won't open     
     {       
        cout << "Output file opening failed.\n";      
        exit(1);  // if the file did not open, program ends 
     }

4.  Use the stream out to send output to the file output.dat just like you would use cout, and it will print to the file just like cout prints to the screen:

     out << "The number is: " << num;  
     // if num=10, this line of code writes to the file "The number is 10"

5.  Don't forget to close the file!
     out.close();  // close output file       

 

Back to Top
4.  Changing format of numbers to display 2 decimal points
Question:
How do I change the format of my numbers to display only 2 decimal points? 

 

Answer:
To make numbers display 2 decimal points, you have to include the <iomanip.h> library.  The following lines of code will produce the desired effect, and will display 10.3 as 10.30.

    #include <iomanip.h>

    cout.setf(ios::fixed); 
    cout.setf(ios::showpoint); 
    cout << setprecision(2) << 10.3 << endl;

 

Back to Top
5.  Difference between get(c) and cin >> c
Question:
What is the difference between get(c) and cin >> c? 

 

Answer:
Every input stream has a member function, get, that can be used to read one character of input.  Unlike the extraction operator, >>, get reads the next input character, no matter what that character is.  In particular, get will read a blank or the new line character, '\n', if either of these are the next input character.  The function get takes one argument which should be a variable of type char.  When get is called, the next input character is read, and the argument variable has its value set equal to this input character.

Syntax:

InputStream.get(char_variable);

Example:

char next_symbol;
cin.get(next_symbol);

If you wish get to read from a file, you use an input file stream in place of the stream cin.  For example, if in_stream is an input stream for a file, then the following will read one character from the input file and place the character in the char variable next_symbol.

in_stream.get(next_symbol);

Before you can use get with an input file stream such as in_stream, your program must first connect the stream to the input file with a call to open.  See FAQ #2 for instructions on how to use input file streams.

Back to Top
6.  Difference between put(c) and cout << c
Question:
What is the difference between put(c) and cout << c? 

 

Answer:
Every output stream has a member function named put.  The member function put takes one argument which should be an expression of type char.  When the member function put is called the value of its argument is output to the output stream.

Syntax:

OutputStream.put(char_expression);

Examples:

cout.put(next_symbol);
cout.put('a');

If you wish to use put to output to a file, you use an output filestream in place of the stream cout.  For example, if out_stream is an output stream for a file, then the following will output the character 'Z' to the file connected to out_stream:

out_stream.put('Z');

Before you can use put with an output filestream, such as out_stream, your program must first connect the stream to the output file with a call to the member function open.  See FAQ #3 for instructions on how to use output file streams.

 

Back to Top