ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1...

15
ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2 2η Σειρά Ασκήσεων Παράδοση έως: 23-03-2020, 23:59:59

Transcript of ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1...

Page 1: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2

2η Σειρά Ασκήσεων

Παράδοση έως: 23-03-2020, 23:59:59

Page 2: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Overview

■ Χρήση command line arguments για είσοδο κριτηρίου

■ Διάβασμα/γράψιμο αρχείων κειμένου

■ Μετατροπή χαρακτήρων σε τιμές (αριθμούς) ASCII

Page 3: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Command line arguments 1/2

Page 4: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Command line arguments 2/2

Page 5: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

C++ Files

■ The fstream library allows us to work with files.

■ ifstream input file stream

■ ofstream output file stream

Object/Data Type Description

ofstream Creates and write to files

ifstream Reads from files

#include <fstream>

#include <iostream>

Page 6: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Read a File

// Create a text string, which is used to output the text

file

string myText;

// Read from the text file

ifstream MyReadFile("filename.txt");

// Use a while loop together with the getline() function

to read the file line by line

while (getline (MyReadFile, myText)) {

// Output the text from the file

cout << myText;

}

// Close the file

MyReadFile.close();

To read from a file, use either the ifstream

object, and the name of the file.

Note that we also use a while loop together

with the getline() function (which belongs

to the ifstream object) to read the file line

by line, and to print the content of the file

// Check is file is open!!if (ifs.is_open()) {

// read file}else {

// show message:cout << "Error opening file";

}

Page 7: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Read a file

ifstream file;

file.open ("filename.txt");

if (file.is_open())

{

string word;

// This loop reads file word by word until

while (file >> word)

{

cout<< word << '\n';

}

}

else

{

………

}

Page 8: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Create and Write to a File

// Create and open a text file

std::ofstream ofs ("filename.txt");

if (ofs.is_open())

{

//write to file

ofs << "lorem ipsum";

ofs.close();

}

else

{

std::cout << "Error opening file";

}

}

To create a file, use either the ofstream

object, and specify the name of the file.

To write to the file, use the insertion

operator (<<).

// Create and open a text filestd::ofstream ofs ;ofs.open ("test.txt");

if (ofs.is_open()){

……

Page 9: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Append to a file

Append to file means that all

output operations happen at the

end of the file, appending to its

existing contents.

ofstream ofs;ofs.open ( "test.txt", std::ofstream::out | std::ofstream::app);

ofs << " write data \n";ofs.close();

Execution 1 Execution 2 Execution 3

write data write data

write data

write data

write data

write data

Note that if file does not exists, it will be

created

Page 10: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Χαρακτήρες ASCII

Printing the decimal representation of character:

Printing the ASCII character which the number

corresponds

char a = 'a';

int ia = (int)a;

Convert char to ASCII number

Page 11: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Strings

// Include the string library#include <string>

// Create a string variablestring greeting = "Hello";

Strings are objects that represent

sequences of characters.

The standard string class provides

support for such objects with an

interface similar to that of a standard

container of bytes, but adding features

specifically designed to operate with

strings of single-byte characters.

Page 12: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

String operations

string str( "HELLO“ );for (int i = 0; i < str.size(); i++){

cout << str[i];//or use str.at(i)

}

Parse string as characters

string name ( "John" );string family ( "Smith" );

name += " K. " ; // stringname += family; // stringname += '\n' ; // character

cout << name;//prints => John K. Smith

Parse string as characters

#include <locale> // std::tolower…….string str= "Test2 #S2tring.\n" ;for (int i = 0; i < str.size(); i++){

cout << (char)std::tolower(str[i]);}

Output: test2 #s2tring.

Print string characters to lowercase

Page 13: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Οδηγίες Άσκησης

■ Το διάβασμα και η εγγραφή σε αρχεία θα πρέπει να γίνεται με την βιβλιοθήκη fstream (ofstream, ifstream)

■ Θεωρήστε ότι τα αρχεία που δίνονται στο πρόγραμμα είναι σωστά και δεν περιέχουν λάθη

■ Το πρόγραμμα θα πρέπει να υποστηρίζει είσοδο μόνο από παραμέτρους (command line arguments)

■ Θα πρέπει να γίνεται έλεγχος ορθότητας των παραμέτρων

■ Θα πρέπει να υποστηρίζονται πολλαπλές λειτουργίες βάση των παραμέτρων (π.χ. ./a.out -p file.txt encoded –f file.txt word1)

■ Οι λέξεις θα πρέπει να αποθηκεύονται σε strings

■ Σε περίπτωση επιτυχίας ή αποτυχίας εκτέλεσης του προγράμματος θα πρέπει να εμφανίζονται τα κατάλληλα μηνύματα

Page 14: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Τρόπος Παράδοσης Ασκήσεων

Με χρήση του turn-in:

turnin assign2@hy150b hw2

Προθεσμία: 23 / 3 / 2020

Για περισσότερες πληροφορίες μπορείτε να δείτε τις οδηγίες στην επίσημη σελίδα του

τμήματος

https://www.csd.uoc.gr/index.jsp?custom=use_the_turnin

Page 15: ΗΥ150 ΦΡΟΝΤΙΣΤΗΡΙΟ 2hy150b/spring-2020/files/tutorial/hy150b_fr… · Execution 1 Execution 2 Execution 3 write data write data write data write data write data write

Thank you