myFile
so the owner has read, write, and execute permissions, group members have read and execute permission, and others have execute permission. (05%)
shell> chmod 751 myFile
stdio.h/cstdio
of C/C++. (05%)
#include <fstream> #include <iostream> using namespace std; int main( ) { fstream file; file.open( "hw1.txt", fstream::out ); file << "This is only a test."; file.close( ); }Write another C/C++ program to open the file and read the last ten bytes of the string and put it into the variable
myString
. (15%)#include <fstream> #include <iostream> using namespace std; int main( ) { fstream file; char myString[11]; int length; file.open( "hw1.txt", fstream::in ); file.seekg( 0, ios::end ); length = file.tellg( ); file.seekg( length-10, ios::beg ); file >> myString; myString[10] = '\0'; file.close( ); cout << myString << endl; }
stdout
. (15%)#include <fstream> #include <iostream> #include <cstring> using namespace std; int main( int argc, char *args[ ] ) { fstream file; char ch; int i = 0, total = 0, line; file.open( args[2], fstream::in ); while ( 1 ) { if ( ( ch = file.get( ) ) == '\n' ) total++; if ( file.eof( ) ) break; } file.close( ); file.open( args[2], fstream::in ); strcpy( args[1], args[1]+1 ); line = atoi( args[1] ); while ( 1 ) if ( ( ch = file.get( ) ) == '\n' ) if ( ++i == total-line ) break; while ( 1 ) { ch = file.get( ); if ( file.eof( ) ) break; cout << ch; } file.close( ); }