Reading from text file and displaying it on screen

I currently trying to read from a text file after which the contents of the text file will be display on the screen with some formatting.

Codes compile with no errors. But when the program is run nothing is display.

Attached below is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* 
 * File:   Users_log.cpp
 * Author: Alpha
 *
 * Created on May 22, 2011, 2:08 PM
 */


#include "Users_log.h"
#include <iostream>

using namespace std;

// Constructor
Userslog::Userslog(string action, string time, string name, string uid, string clearance, string comment, string status)
{
    this->action = action;
    this->time = time;
    this->name = name;
    this->uid = uid;
    this->clearance = clearance;
    this->comment = comment;
    this->status = status;

}

// Get Methods
string Userslog::getAction(){ return this->action; }
string Userslog::getTime(){ return this->time; }
string Userslog::getName(){ return this->name; }
string Userslog::getUID() { return this->uid; }
string Userslog::getClearance() { return this->clearance; }
string Userslog::getComment() { return this->comment; }
string Userslog::getStatus() { return this->status; }

// Set Methods
void Userslog::setAction(string val){ this->action = val; }
void Userslog::setTime(string val){ this->time = val; }
void Userslog::setName(string val){ this->name = val; }
void Userslog::setUID(string val){ this->uid = val; }
void Userslog::setClearance(string val){ this->clearance = val; }
void Userslog::setComment(string val){ this->comment = val; }
void Userslog::setStatus(string val){ this->status = val; }

// Other Methods
void Userslog::display()
{
    cout << "Item: " << this->action << endl;
    cout << "Time: " << this->time << endl;
  
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* 
 * File:   Users_log.h
 * Author: Alpha
 *
 * Created on May 22, 2011, 2:02 PM
 */

#include <string>
#include <cstdlib>

using namespace std;

#ifndef USERS_LOG_H
#define	USERS_LOG_H

class Userslog {
public:
    // Constructor
    Userslog(string, string, string, string, string, string, string);

    // Get Methods
    string getAction();
    string getTime();
    string getName();
    string getUID();
    string getClearance();
    string getComment();
    string getStatus();

    // Set Methods
    void setAction(string);
    void setTime(string);
    void setName(string);
    void setUID(string);
    void setClearance(string);
    void setComment(string);
    void setStatus(string);
    
    // Other Methods
    void display();
    
private:
    string action;
    string time;
    string name;
    string uid;
    string clearance;
    string comment;
    string status;
};

#endif	/* USERS_LOG_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* 
 * File:   main.cpp
 * Author: Alpha
 *
 * Created on May 22, 2011, 4:53 PM
 */

#include <cstdlib>
#include <string>
#include "Users_log.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <bits/basic_string.h>

using namespace std;

vector<Userslog> userslog;

//Functions
void loadUsersLog();
void display();

/*******************************************************************************/
/* Users_log Functions                                                         */
/*******************************************************************************/

void loadUsersLog()
{
    string readLine; // To read everyline
    size_t pointer_str; // Find the position of string

    string temp_action, temp_time, temp_name, temp_uid, temp_clearance, temp_comment,temp_status;
    
    // Open the file for read only
    ifstream openFile;
    openFile.open("Users_log.txt", ios::in); // Read the data file
    
       if(openFile.is_open()){
        string temp;
        openFile.seekg(0, ios::end);
        if (openFile.tellg() == 0)
            openFile.close();
        else {
            openFile.seekg (0, ios::beg);
            while(!openFile.eof()){
                getline(openFile,readLine);
                pointer_str = readLine.find(":");
                temp = readLine.substr(pointer_str + 1);
                temp_action = readLine.substr(0, pointer_str);

                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_time = readLine.substr(0, pointer_str);

                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_name = readLine.substr(0, pointer_str);

                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_uid = temp.substr(0, pointer_str);
                
                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_clearance = temp.substr(0, pointer_str);
                
                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_comment = temp.substr(0, pointer_str);
                
                pointer_str = temp.find(":");
                temp = temp.substr(pointer_str + 1);
                temp_status = temp.substr(0, pointer_str);
                

                if(temp_action != ""){
                    userslog.push_back(Userslog(temp_action, temp_time, temp_name, temp_uid, temp_clearance, temp_comment,temp_status));
                }
            }
            // Must close the file
            openFile.close();
        }
    } else {
	// Show error message, if could not open the file
        cout << "Error: Could not open the file." << endl;
    }
}
    
// Function display: display users log
void display()
{
    cout << "No.\tItem\t\tTime" << endl;
    cout << "===============================================================================" << endl;
    int counter = 1;
    vector<Userslog>::iterator it;
    for (it = userslog.begin(); it != userslog.end(); ++it) {
        cout << counter << ".\t" << it->getAction() <<  "\t\t" << it->getTime() << "\t\t" << it->getName() << "\t\t" << it->getUID() << endl;
        counter++;
    }
}

int main() 
{
    loadUsersLog();
    return 0;
}



¿where did you call at display?

1
2
3
4
5
6
                getline(openFile,readLine);
                pointer_str = readLine.find(":");
                temp = readLine.substr(pointer_str + 1);
                temp_action = readLine.substr(0, pointer_str); 

getline(openFile, readLine, ':'); //¿Is this what you want? 


Also
1
2
3
4
5
6
7
8
9
10
11
12
struct Userslog{ //all public
    Userslog(string, string, string, string, string, string, string);
    void display();

    string action;
    string time;
    string name;
    string uid;
    string clearance;
    string comment;
    string status;
};


1
2
3
4
5
6
7
8
//¿what is the point here?
        openFile.seekg(0, ios::end);
        if (openFile.tellg() == 0)
            openFile.close();


            while(!openFile.eof()){
                getline(openFile,readLine); //what if you reach eof here 
Last edited on
Hi,

this is the txt file I trying to read in, which I want to format it into the output like below.

0:10:5:Fred:3:New client:1:
1:15:1:Bob:Retired:1:
2:25:5:Fred:2:Wrong clearance initially:1:


---
Time: 10 Operation succeeded
Added: Fred user_id: 5 Clearance: 3
Comment: New Client
---
Time: 15 Operation succeeded
Added: Bob user_id: 3
Comment: Retired
---
Time: 25 Operation succeeded
Added: Fred user_id: 5 Clearance: 2
Comment: Wrong clearance initially


Topic archived. No new replies allowed.