How to edit specific part of line from a text file

closed account (426pfSEw)
Hello everyone,

So what I'm trying to do is read data from a file, to then edit specific portions of it. Two types of people will be viewing it (the "employee" and "the manager"). The "employee" can only edit the first 5 words, while the "manager" can edit anything.

The problem I am having is after I get it into an array, either all of the info in one line gets put in without spaces in between them, making it so I can't call the line to edit it. Or there are too many spaces added (after several views) where it crashes the program (also, still cannot read the line from the array to edit it).

Here's the code I have so far...


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void editEm(string user,string pass){
ifstream datafile,newfile;
ofstream newdata,oldfile;
string in,line;
string words[100][10];
int i(0),j(0),f(0);

cout<<"Please confirm employee status: "<<endl;
cin>>in;
cout<<endl<<endl;

if(user == in){

datafile.open("database.txt");
if(datafile.fail()){
cerr<<"File not found"<<endl;
exit(1);
}

newdata.open("Ndatabase.txt");
if(newdata.fail()){
cerr<<"File not found"<<endl;
exit(1);
}

while(!datafile.eof()){
f++;
getline(datafile,line);
newdata<<line<<endl;
//cout<<line<<endl;
}

datafile.close();
newdata.close();

newfile.open("Ndatabase.txt");
if(newfile.fail()){
cerr<<"File not found"<<endl;
exit(1);
}

oldfile.open("database.txt");
if(oldfile.fail()){
cerr<<"File not found"<<endl;
exit(1);
}
/*************************************************************************/
/*************************************************************************/
//Everything above this point is good, opening, reading, and putting data into temp file.
//Everything below is what I was trying to work with to edit. I was trying to put the
//info into an array so we can search for a line, then output what information they would
//be allowed to change. I have it going into the array, but if you want spaces to actually
//read the file it eventually crashes the program becasue of too much space, along with the
//fact it doesn't read the input to compare to the line. While the other way where it doesn't crash
//puts no spaces between each word in the line, but seperates the line. However, because there are
//no spaces, the program cannot read the line.

for(i=0;i<(f/2);i++){
for(j=0;j<10;j++){
if(getline(newfile,line,' ')){
//cout<<line<<" ";
if(in != line+" "){
words[i][j] = line;
//cout<<in<<endl<<endl;
cout<<line;
}
else{
cout<<in<<endl;
for(j=0;j<10;j++){
cout<<j+1<<"\t"<<words[i][j];
}
}
}
oldfile<<words[i][j];
}
}

//This text just puts the info into an array and back to the original file, I don't see
//any way to change info from here, but I kept it in case I need to start from scratch.

/*for(i=0;i<(f/2);i++){
for(j=0;j<10;j++){
newfile>>words[i][j];
}
}

for(i=0;i<(f/2);i++){
for(j=0;j<10;j++){
oldfile<<words[i][j]<<" ";
cout<<words[i][j]<<" ";
}
oldfile<<endl<<endl;
cout<<endl;
} */


newfile.close();
oldfile.close();
newfile.open("Ndatabase.txt");
newfile.close();
}
}


I've been raking my brain for hours and cannot seem to figure out how to make it work. Thanks for taking the time to help.
Last edited on
*allowed
closed account (426pfSEw)
Oh, sorry. Fixed that, but any other help pogrady?
Topic archived. No new replies allowed.