FAQ ~ Dr. Tashakkori's 1440 Classes

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

1.  Going to a specific line in pico and vile
2.  How to determine what line you are on in pico
3.  Difference between "r" and "R" in Pine
4.  Attaching a file to an email in Pine
 .  Compiler error question
 .  Reading data from a file
 .  Writing output to a file
 .  Changing format of numbers to display 2 decimal points
 .  Difference between get(c) and cin >> c
 .  Difference between put(c) and cout << c

1.  Going to a specific line in pico
Question:
The compiler says there's an error on line 42. How can I get to that line quickly using pico?

Answer:
You can quickly go to line 42 of your file from the Unix prompt by typing:
% pico +42 yourfilename.C

If you are already in the file, CTRL-C tells you what line number you are currently on in pico. 

Back to Top

2.  How to determine what line you are on in pico
Question:
How do I know what line the cursor is currently on when I'm using pico?

 

Answer:
CTRL-C tells you the line number in pico.  This can be useful when finding a line that caused an error. Press CTRL-V to go one page down or CTRL-Y to go one page up.
Back to Top
3.  Difference between "r" and "R" in pine
Question:
When replying to an email message in pine, what is the difference between using "r" and "R"?

 

Answer:
When using pine, replying with little "r" will reply to everyone who received the original email, but if you use capital "R", you will only reply to the sender of the email.

Suppose Dr. Tashakkori had sent an email to several students in your class including you.  You have two options for replying to that email: "R" or "r".  If you use "R", you will reply to Dr. Tashakkori's email and he will be the only one who receives your e-mail.  But if you use "r", everyone in the class who received the original email will get a copy of your reply to Dr. Tashakkori.  

It is best to use "R" when replying to email.

Back to Top
4.  Attaching a file to an email in Pine
Question:
How do I attach a file to an email using Pine?
Answer:
Let's assume that your username is xy12345 and you want to attach a file called "prog1.C" from your 1440 directory to an email you have already composed.

a.  Move your cursor to the line in the top of the message labeled Attchmnt.  It's just above the Subject line.  (You cannot attach any files unless you put your cursor on this line!)

b.  Here you have two choices, listed below.

1.  Type in the path and filename on the Attchmnt: line 
     Attchmnt:   ~xy12345/1440/prog1.C and hit Enter.
2.  Hit Ctrl - T (this will take you to all the files in your home directory).
     Move your cursor to 1440 directory listed there, and hit Enter.

     Move your cursor to the file "prog1.C" and hit Enter. 

cYou will be prompted to name the Attachment, call it whatever you like and hit Enter.

d.  Hit Ctrl - X to send the email as usual.

Back to Top

 

 

 

 

 

 

 

 

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

.  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
.  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
.  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
.  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
.  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