Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First Unix Program

Status
Not open for further replies.

UOB71

Programmer
Apr 17, 2003
1
US
Hello everyone

I am new in Unix programming, I tried my C programming background, but didn't work, can you help, Please??

I would like to create a program to do the following in Unix:

-Read a text file and print it line by line.
-Read your directory (under any name) and display the content of it whether it is ,file or a directory

 
Read a text file line by line:
Code:
#include <iostream>
#include <fstream>
#include <string>

int main()
{
  cout << &quot;Enter file name:&quot;; //print prompt
  string s;
  cin s;
  fstream fin; //decleare a stream
  fin.open(s.c_str()); //open the file
  string line;
  while(!fin.bad()) //loop
  {
     getline(fin,line,&quot;\n&quot;); //get the line
     cout << line; //print the line
  }
  fin.close(); //close the file
  return 0;
}

I am running late, so that is all I have time to post.. I didn't compile that, so I may have done a silly mistake, but it should be good, minus lack of error checking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top