Hello guys!

can you give me an example on how to switch lines or records inside a textfile? I need to enter 2 numbers, there are 3 numbers inside the textfile, which also means it has 3 lines.

1 name
2 name
3 name

1st number entered= 2
2nd number entered=1

after entering those numbers, the notepad should look like this

2 name
1 name
3 name

I think this needs looping and array, and i'm not that good in making loops or array.

Here's my read code btw,

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using std::fixed;
using std::ios;
using std::left;
using std::right;
using std::showpoint;

#include <fstream>
using std::ifstream;

#include<iomanip>
using std::setw;
using std::setprecision;

#include <string>
using std::string;

#include <cstdlib>

void outputLine( int, const string );


int main()


{
    ifstream inClientFile("clients.txt", ios::in );

    if ( !inClientFile )

    {
        cerr<< "File could not be opened"<<endl;
        exit( 1 );
    }

    int account;
    char name[30];

    

    cout<< left << setw( 10 ) << "Account" << setw( 13 )
        << "Name"<< endl << fixed << showpoint;



    while ( inClientFile >> account >> name )
        outputLine( account, name);

    return 0;

}

void outputLine( int account, const string name )
{

    cout << left << setw(10) << account << setw(13) << name << endl;
}

I really need a good example that will help me understand how to do it.. thanks!

Recommended Answers

All 2 Replies

Here is suggested pseudo code for what ye' want to accomplish:

1. create an ifstream object (for opening & reading from file)
2. create an ofstream object (to write to the file)
3. open the source .txt file
4. perform error checking (see if file opened properly & if it even exists)
5. read in your text file (preferably line-by-line for this project using getline())
6. populate an array of strings[3] (each element will hold a line of text)
7. close the ifstream object
8. Prompt for user input
9. Peform desired operations on the string[3] array (based on stored user input)
9. Write string[3] to the .txt file
10. close ofstream object

I can now store data from a textfile to array of strings. But don't know how to compare the input number to the value of the array string to switch places/line inside the notepad. Here's my store data array string code btw,

#include <iostream>
#include <fstream>
#include <sstream>
#include <list>

using namespace std;

// Structure for containing data of multiple datatypes
struct Person {
    string Name;
    int AccountNumber;
};

// Needed to signal an error
struct PersonsException {};

class Persons {

    private:
        string m_Filename; // Contains filename
        ifstream m_FileStream; // Contains file
        list<Person> m_List; // List for storage

    public:
        Persons(string Filename) { m_Filename = Filename; }
        int readFile();
        void displayList();
};

int Persons::readFile() {
    
    
    // Open file in filestream
    m_FileStream.open(m_Filename.c_str());
    
    // If no file open, throw an exception - function is terminated
    if (!m_FileStream.is_open()) throw PersonsException();
    
    // Read lines until end of file is reached
    while (!m_FileStream.eof()) {
          string Line;
          
          // Read line from file and put into string
          getline(m_FileStream, Line);
          
          // If string is empty, ignore it
          if (Line.empty()) continue;
          
          // Put string into input stringstream          
          istringstream Stream(Line);
          
          // Create a structure object to use for storing the data
          Person Entry;
          
          // Extract data from istringstream and put into structure         
          Stream >> Entry.AccountNumber >> Entry.Name;
          
          // Add structure to list
          m_List.push_back(Entry);
    }
    
    // Return size of the list
    return m_List.size();
}

void Persons::displayList() {
    // Ready iterator for list
    list<Person>::iterator i;
    
    // Iterate through the list
    for ( i = m_List.begin(); i != m_List.end(); ++i) {
        // Print each entry of the list
        cout << i->AccountNumber <<" "<< i->Name << endl;
    }
}


int main(int argc, char **argv) {
    // Try the class out
    try {
        Persons obj1("clients.txt");
        obj1.readFile();
        obj1.displayList();
    }
    // Oops, an error was thrown
    catch (PersonsException &e) {
          cout << "An error occured - file may not exist." << endl;      
    }
 
    system("pause");
    
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.