Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help with Edit File

TheFreeForAllTheFreeForAll Posts: 7Member
I need help with this function Edit File of a program i am working on.
This is what it should do...
You have a text file created like this for example
10
20
30
40
50
This function should let you choose what line you want to edit...
For example you want to change line three and make it say 12 so now the file should look like
10
20
12
40
50

This is the code (Im putting the entire program in here incase something else needs to be changed)

#include
#include
#include
#include

void CreateTextFile(String & FileName, String Data);
void CreateNumberFile(String FileName,int NumScores,double Score,double Sum);
void ViewFile(String FileName,String Text);
void RenameFile(String NewName,String FileName);
void DeleteFile(String FileName);
void EditFile(String FileName,String S,int LineNum,int charcount,double newdata);
void AppendFile(String FileName,String ExtraCode,String Text);

int main()
{
int LineNum=0;
int NumScores=0;
double Score=0;
double Sum=0;
double newdata=0;
char choice;
int charcount=0;
String S;
String FileName;
String Data;
String Text;
String repeat;
String NewName;
String ExtraCode;
do{
cout<<"Welcome to the file creator Program."<<endl;
cout<<"------------------------------------"<<endl;
cout<<"Press 1 to Create a Text File."<<endl;
cout<<"Press 2 to Create a Number File."<<endl;
cout<<"Press 3 to View a File."<<endl;
cout<<"Press 4 to Rename a File."<<endl;
cout<<"Press 5 to Delete a File."<<endl;
cout<<"Press 6 to Edit a File."<<endl;
cout<<"Press 7 to Append a File."<<endl;
cout<<endl;
cout<<"Enter Choice:";
cin>>choice;
cout<<endl;
if(choice=='1')
CreateTextFile(FileName,Data);
else if(choice=='2')
CreateNumberFile(FileName,NumScores,Score,Sum);
else if(choice=='3')
ViewFile(FileName,Text);
else if(choice=='4')
RenameFile(NewName,FileName);
else if(choice=='5')
DeleteFile(FileName);
else if(choice=='6')
EditFile(FileName,S,LineNum,charcount,newdata);
else if(choice=='7')
AppendFile(FileName,ExtraCode,Text);
else
{
cout<<"Invalid Choice"<<endl;
cout<<"Re-enter Choice:"<<endl;
cin>>choice;
}
cout<<"Would you like to choose another selection:(yes/no)";
cin>>repeat;
system("cls");
}while(repeat=="yes");
return(0);
}
//------------------------------------------------------------------
void CreateTextFile(String & FileName, String Data)
{
cout<<"Name of the File you would like to make:";
cin.ignore();
getline(cin,FileName);
ofstream OutFile;
OutFile.open(FileName.c_str());
if(OutFile.fail())
{
cout<<"File has not been written."<<endl;
}
else
{
cout<<"Enter text of Code."<<endl;
while(Data!="end")
{
getline(cin,Data);
OutFile<<Data<<endl;
}
}
}
//------------------------------------------------------------------
void CreateNumberFile(String FileName,int NumScores,double Score,double Sum)
{
cout<<"Name of File you would like to make:";
cin.ignore();
getline(cin,FileName);
ofstream OutFile;
OutFile.open(FileName.c_str());
cin.ignore();
if(OutFile.fail())
{
cout<<"File has not been written."<<endl;
}
else
{
cout<<"--------------------------------------------------"<<endl;
cout<<"--------------------SCORES------------------------"<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"Enter Number of Scores:";
cin>>NumScores;
for(int i=1;i<=NumScores;i++)
{
cout<<"Enter Score "<<i<<":";
cin>>Score;
OutFile<<i<<"."<<Score<<endl;
Sum+=Score;
}
cout<<"Number of Scores:"<<NumScores<<endl;
cout<<"Total of Scores:"<<Sum<<endl;
cout<<"Average of Scores:"<<(Sum/NumScores)<<endl;
}
}
//----------------------------------------------------------------
void ViewFile(String FileName,String Text)
{
cout<<"What is the Name of the File you would like to read."<<endl;
cin.ignore();
getline(cin,FileName);
ifstream InFile(FileName.c_str(),ios::nocreate);
if(InFile.fail())
{
cout<<"Program Can Not Be Read."<<endl;
}
else
{
while(getline(InFile,Text))
{
cout<<">"<<Text<<endl;
}
}
}
//-------------------------------------------------------------------------
void RenameFile(String NewName,String FileName)
{
cout<<"What File would you like to rename:";
cin.ignore();
getline(cin,FileName);
cout<<"What Would you like the new file Name to be:";
getline(cin,NewName);
rename(FileName.c_str(),NewName.c_str());
}
//--------------------------------------------------------------------------
void DeleteFile(String FileName)
{
cout<<"What File would you like to delete:"<<endl;
cin.ignore();
getline(cin,FileName);
remove(FileName.c_str());
if(remove(FileName.c_str())==0)
cout<<"File has not been deleted."<<endl;
else
cout<<FileName<<" has been removed"<<endl;
}
//------------------------------------------------------------
void EditFile(String FileName,String S,int LineNum,int charcount,double newdata)
{
fstream EditFile(FileName.c_str(),ios::in|ios::out);
cout<<"What File would you like to edit:"<<endl;
cin.ignore();
getline(cin,FileName);
fstream InFile;
InFile.open(FileName.c_str(),ios::in|ios::out);
while(getline(InFile,S))
{
cout<<S<<endl;
}
cout<<"What Line do you want to change:";
cin>>LineNum;
for(int i=0;i>newdata;
EditFile.seekp(charcount);
EditFile<<newdata;
EditFile.close();
}
//-----------------------------------------------------------------
void AppendFile(String FileName,String ExtraCode,String Text)
{
cout<<"Enter File to Append to."<<endl;
cin>>FileName;
fstream AppendFile;
AppendFile.open(FileName.c_str(),ios::in|ios::out);
while(getline(AppendFile,Text))
{
cout<<">"<<Text<<endl;
}

AppendFile.close();
AppendFile.open(FileName.c_str(),ios::in|ios::out);
AppendFile.seekp(0, ios::end);
cout<<"Enter New Lines of Code."<<endl;
while(ExtraCode!="end")
{
getline(cin,ExtraCode);
AppendFile<<ExtraCode<<endl;
}

AppendFile.close();
}
«1

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    edit your post to use code tags -- nobody wants to read all that unformatted code :-(
  • TheFreeForAllTheFreeForAll Posts: 7Member
    : edit your post to use code tags -- nobody wants to read all that unformatted code :-(
    :
    what is a code tag?
  • stoberstober Posts: 9,765Member ✭✭✭
    : : edit your post to use code tags -- nobody wants to read all that unformatted code :-(
    : :
    : what is a code tag?
    :

    see the section named [b]Outputting code or fixed width data[/b]

    http://www.phpbb.com/phpBB/faq.php?mode=bbcode#5

  • TheFreeForAllTheFreeForAll Posts: 7Member
    I need help with this function Edit File of a program i am working on.
    This is what it should do...
    You have a text file created like this for example
    10
    20
    30
    40
    50
    This function should let you choose what line you want to edit...
    For example you want to change line three and make it say 12 so now the file should look like
    10
    20
    12
    40
    50

    This is the code (Im putting the entire program in here incase something else needs to be changed)

    [code]#include
    #include
    #include
    #include

    void CreateTextFile(String & FileName, String Data);
    void CreateNumberFile(String FileName,int NumScores,double Score,double Sum);
    void ViewFile(String FileName,String Text);
    void RenameFile(String NewName,String FileName);
    void DeleteFile(String FileName);
    void EditFile(String FileName,String S,int LineNum,int charcount,double newdata);
    void AppendFile(String FileName,String ExtraCode,String Text);

    int main()
    {
    int LineNum=0;
    int NumScores=0;
    double Score=0;
    double Sum=0;
    double newdata=0;
    char choice;
    int charcount=0;
    String S;
    String FileName;
    String Data;
    String Text;
    String repeat;
    String NewName;
    String ExtraCode;
    do{
    cout<<"Welcome to the file creator Program."<<endl;
    cout<<"------------------------------------"<<endl;
    cout<<"Press 1 to Create a Text File."<<endl;
    cout<<"Press 2 to Create a Number File."<<endl;
    cout<<"Press 3 to View a File."<<endl;
    cout<<"Press 4 to Rename a File."<<endl;
    cout<<"Press 5 to Delete a File."<<endl;
    cout<<"Press 6 to Edit a File."<<endl;
    cout<<"Press 7 to Append a File."<<endl;
    cout<<endl;
    cout<<"Enter Choice:";
    cin>>choice;
    cout<<endl;
    if(choice=='1')
    CreateTextFile(FileName,Data);
    else if(choice=='2')
    CreateNumberFile(FileName,NumScores,Score,Sum);
    else if(choice=='3')
    ViewFile(FileName,Text);
    else if(choice=='4')
    RenameFile(NewName,FileName);
    else if(choice=='5')
    DeleteFile(FileName);
    else if(choice=='6')
    EditFile(FileName,S,LineNum,charcount,newdata);
    else if(choice=='7')
    AppendFile(FileName,ExtraCode,Text);
    else
    {
    cout<<"Invalid Choice"<<endl;
    cout<<"Re-enter Choice:"<<endl;
    cin>>choice;
    }
    cout<<"Would you like to choose another selection:(yes/no)";
    cin>>repeat;
    system("cls");
    }while(repeat=="yes");
    return(0);
    }
    //------------------------------------------------------------------
    void CreateTextFile(String & FileName, String Data)
    {
    cout<<"Name of the File you would like to make:";
    cin.ignore();
    getline(cin,FileName);
    ofstream OutFile;
    OutFile.open(FileName.c_str());
    if(OutFile.fail())
    {
    cout<<"File has not been written."<<endl;
    }
    else
    {
    cout<<"Enter text of Code."<<endl;
    while(Data!="end")
    {
    getline(cin,Data);
    OutFile<<Data<<endl;
    }
    }
    }
    //------------------------------------------------------------------
    void CreateNumberFile(String FileName,int NumScores,double Score,double Sum)
    {
    cout<<"Name of File you would like to make:";
    cin.ignore();
    getline(cin,FileName);
    ofstream OutFile;
    OutFile.open(FileName.c_str());
    cin.ignore();
    if(OutFile.fail())
    {
    cout<<"File has not been written."<<endl;
    }
    else
    {
    cout<<"--------------------------------------------------"<<endl;
    cout<<"--------------------SCORES------------------------"<<endl;
    cout<<"--------------------------------------------------"<<endl;
    cout<<"Enter Number of Scores:";
    cin>>NumScores;
    for(int i=1;i<=NumScores;i++)
    {
    cout<<"Enter Score "<<i<<":";
    cin>>Score;
    OutFile<<i<<"."<<Score<<endl;
    Sum+=Score;
    }
    cout<<"Number of Scores:"<<NumScores<<endl;
    cout<<"Total of Scores:"<<Sum<<endl;
    cout<<"Average of Scores:"<<(Sum/NumScores)<<endl;
    }
    }
    //----------------------------------------------------------------
    void ViewFile(String FileName,String Text)
    {
    cout<<"What is the Name of the File you would like to read."<<endl;
    cin.ignore();
    getline(cin,FileName);
    ifstream InFile(FileName.c_str(),ios::nocreate);
    if(InFile.fail())
    {
    cout<<"Program Can Not Be Read."<<endl;
    }
    else
    {
    while(getline(InFile,Text))
    {
    cout<<">"<<Text<<endl;
    }
    }
    }
    //-------------------------------------------------------------------------
    void RenameFile(String NewName,String FileName)
    {
    cout<<"What File would you like to rename:";
    cin.ignore();
    getline(cin,FileName);
    cout<<"What Would you like the new file Name to be:";
    getline(cin,NewName);
    rename(FileName.c_str(),NewName.c_str());
    }
    //--------------------------------------------------------------------------
    void DeleteFile(String FileName)
    {
    cout<<"What File would you like to delete:"<<endl;
    cin.ignore();
    getline(cin,FileName);
    remove(FileName.c_str());
    if(remove(FileName.c_str())==0)
    cout<<"File has not been deleted."<<endl;
    else
    cout<<FileName<<" has been removed"<<endl;
    }
    //------------------------------------------------------------
    void EditFile(String FileName,String S,int LineNum,int charcount,double newdata)
    {
    fstream EditFile(FileName.c_str(),ios::in|ios::out);
    cout<<"What File would you like to edit:"<<endl;
    cin.ignore();
    getline(cin,FileName);
    fstream InFile;
    InFile.open(FileName.c_str(),ios::in|ios::out);
    while(getline(InFile,S))
    {
    cout<<S<<endl;
    }
    cout<<"What Line do you want to change:";
    cin>>LineNum;
    for(int i=0;i>newdata;
    EditFile.seekp(charcount);
    EditFile<<newdata;
    EditFile.close();
    }
    //-----------------------------------------------------------------
    void AppendFile(String FileName,String ExtraCode,String Text)
    {
    cout<<"Enter File to Append to."<<endl;
    cin>>FileName;
    fstream AppendFile;
    AppendFile.open(FileName.c_str(),ios::in|ios::out);
    while(getline(AppendFile,Text))
    {
    cout<<">"<<Text<<endl;
    }

    AppendFile.close();
    AppendFile.open(FileName.c_str(),ios::in|ios::out);
    AppendFile.seekp(0, ios::end);
    cout<<"Enter New Lines of Code."<<endl;
    while(ExtraCode!="end")
    {
    getline(cin,ExtraCode);
    AppendFile<<ExtraCode<<endl;
    }

    AppendFile.close();
    }[/code]

  • stoberstober Posts: 9,765Member ✭✭✭
    well, that's a good start. Do you really code like that? Everything starting in the far-left side of the page with no indentation?
  • TheFreeForAllTheFreeForAll Posts: 7Member
    : well, that's a good start. Do you really code like that? Everything starting in the far-left side of the page with no indentation?
    :

    No I Dont. I guess it just came out that way.
    I keep trying but this is the only way I can post it

    I need help with this function Edit File of a program i am working on.
    This is what it should do...
    You have a text file created like this for example
    10
    20
    30
    40
    50
    This function should let you choose what line you want to edit...
    For example you want to change line three and make it say 12 so now the file should look like
    10
    20
    12
    40
    50

    This is the code (Im putting the entire program in here incase something else needs to be changed)

    int main()
    {
    int LineNum=0;
    int NumScores=0;
    double Score=0;
    double Sum=0;
    double newdata=0;
    char choice;
    int charcount=0;
    String S;
    String FileName;
    String Data;
    String Text;
    String repeat;
    String NewName;
    String ExtraCode;
    do{
    cout<<"Welcome to the file creator Program."<<endl;
    cout<<"------------------------------------"<<endl;
    cout<<"Press 1 to Create a Text File."<<endl;
    cout<<"Press 2 to Create a Number File."<<endl;
    cout<<"Press 3 to View a File."<<endl;
    cout<<"Press 4 to Rename a File."<<endl;
    cout<<"Press 5 to Delete a File."<<endl;
    cout<<"Press 6 to Edit a File."<<endl;
    cout<<"Press 7 to Append a File."<<endl;
    cout<<endl;
    cout<<"Enter Choice:";
    cin>>choice;
    cout<<endl;
    if(choice=='1')
    CreateTextFile(FileName,Data);
    else if(choice=='2')
    CreateNumberFile(FileName,NumScores,Score,Sum);
    else if(choice=='3')
    ViewFile(FileName,Text);
    else if(choice=='4')
    RenameFile(NewName,FileName);
    else if(choice=='5')
    DeleteFile(FileName);
    else if(choice=='6')
    EditFile(FileName,S,LineNum,charcount,newdata);
    else if(choice=='7')
    AppendFile(FileName,ExtraCode,Text);
    else
    {
    cout<<"Invalid Choice"<<endl;
    cout<<"Re-enter Choice:"<<endl;
    cin>>choice;
    }
    cout<<"Would you like to choose another selection:(yes/no)";
    cin>>repeat;
    system("cls");
    }while(repeat=="yes");
    return(0);
    }
    //------------------------------------------------------------------
    void CreateTextFile(String & FileName, String Data)
    {
    cout<<"Name of the File you would like to make:";
    cin.ignore();
    getline(cin,FileName);
    ofstream OutFile;
    OutFile.open(FileName.c_str());
    if(OutFile.fail())
    {
    cout<<"File has not been written."<<endl;
    }
    else
    {
    cout<<"Enter text of Code."<<endl;
    while(Data!="end")
    {
    getline(cin,Data);
    OutFile<<Data<<endl;
    }
    }
    }
    //------------------------------------------------------------------
    void CreateNumberFile(String FileName,int NumScores,double Score,double Sum)
    {
    cout<<"Name of File you would like to make:";
    cin.ignore();
    getline(cin,FileName);
    ofstream OutFile;
    OutFile.open(FileName.c_str());
    cin.ignore();
    if(OutFile.fail())
    {
    cout<<"File has not been written."<<endl;
    }
    else
    {
    cout<<"--------------------------------------------------"<<endl;
    cout<<"--------------------SCORES------------------------"<<endl;
    cout<<"--------------------------------------------------"<<endl;
    cout<<"Enter Number of Scores:";
    cin>>NumScores;
    for(int i=1;i<=NumScores;i++)
    {
    cout<<"Enter Score "<<i<<":";
    cin>>Score;
    OutFile<<i<<"."<<Score<<endl;
    Sum+=Score;
    }
    cout<<"Number of Scores:"<<NumScores<<endl;
    cout<<"Total of Scores:"<<Sum<<endl;
    cout<<"Average of Scores:"<<(Sum/NumScores)<<endl;
    }
    }
    //----------------------------------------------------------------
    void ViewFile(String FileName,String Text)
    {
    cout<<"What is the Name of the File you would like to read."<<endl;
    cin.ignore();
    getline(cin,FileName);
    ifstream InFile(FileName.c_str(),ios::nocreate);
    if(InFile.fail())
    {
    cout<<"Program Can Not Be Read."<<endl;
    }
    else
    {
    while(getline(InFile,Text))
    {
    cout<<">"<<Text<<endl;
    }
    }
    }
    //---------------------------------------------------------------------
    void RenameFile(String NewName,String FileName)
    {
    cout<<"What File would you like to rename:";
    cin.ignore();
    getline(cin,FileName);
    cout<<"What Would you like the new file Name to be:";
    getline(cin,NewName);
    rename(FileName.c_str(),NewName.c_str());
    }
    //---------------------------------------------------------------------
    void DeleteFile(String FileName)
    {
    cout<<"What File would you like to delete:"<<endl;
    cin.ignore();
    getline(cin,FileName);
    remove(FileName.c_str());
    if(remove(FileName.c_str())==0)
    cout<<"File has not been deleted."<<endl;
    else
    cout<<FileName<<" has been removed"<<endl;
    }
    //------------------------------------------------------------
    void EditFile(String FileName,String S,int LineNum,int charcount,double newdata)
    {
    fstream EditFile(FileName.c_str(),ios::in|ios::out);
    cout<<"What File would you like to edit:"<<endl;
    cin.ignore();
    getline(cin,FileName);
    fstream InFile;
    InFile.open(FileName.c_str(),ios::in|ios::out);
    while(getline(InFile,S))
    {
    cout<<S<<endl;
    }
    cout<<"What Line do you want to change:";
    cin>>LineNum;
    for(int i=0;i>newdata;
    EditFile.seekp(charcount);
    EditFile<<newdata;
    EditFile.close();
    }
    //-----------------------------------------------------------------
    void AppendFile(String FileName,String ExtraCode,String Text)
    {
    cout<<"Enter File to Append to."<<endl;
    cin>>FileName;
    fstream AppendFile;
    AppendFile.open(FileName.c_str(),ios::in|ios::out);
    while(getline(AppendFile,Text))
    {
    cout<<">"<<Text<<endl;
    }
    AppendFile.close();
    AppendFile.open(FileName.c_str(),ios::in|ios::out);
    AppendFile.seekp(0, ios::end);
    cout<<"Enter New Lines of Code."<<endl;
    while(ExtraCode!="end")
    {
    getline(cin,ExtraCode);
    AppendFile<<ExtraCode<<endl;
    }
    AppendFile.close();
    }[/code]

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-5-31 20:44:45[/red][/b][hr]
    you don't need all those parameters to that function! If fact, you don't need any parameters at all.

    Here is one way to do it -- there are several other ways. I didn't compile this, so I hope it compiles ok.

    [code]
    #include
    ...
    ...
    void EditFile()
    {
    String FileName;
    String S;
    int LineNum;
    long newdata;

    vector list; // an array of lines
    cout<<"What File would you like to edit:"<<endl;
    getline(cin,FileName);
    ifstream in(FileName.c_str());
    while(getline(in,S))
    {
    list.push_back(S);
    cout<<S<<endl;
    }
    in.close();
    cout<<"What Line do you want to change:";
    cin>>LineNum;

    cout<<"Enter new data:";
    cin>>newdata;
    // convert int to string
    stringstream str;
    str << newdata;
    // put new string into the array
    list[LineNum] = str.str();
    // rewrite the file
    ofstream out.open(FileName.c_str());
    vector<String>::iterator it;
    for(it = list.begin(); it != list.end(); it++)
    {
    out << *it << "
    ";
    }
    out.close();
    [/code]



  • TheFreeForAllTheFreeForAll Posts: 7Member
    is there any way you could try to fix edit file without changing the entire thing and base it off how it already is
  • stoberstober Posts: 9,765Member ✭✭✭
    : is there any way you could try to fix edit file without changing the entire thing and base it off how it already is
    :

    You have to normally rewrite entire text files because they do not have fixed-length records. You can't replace one word with another word of a different size without either leaving some extraneous characters when the new word is shorter than the original word, or without overwiritng parts of the following text when the new word is longer than the original word. When either of these two conditions exist, the only way to change the file is to rewrite the entire thing.
  • TheFreeForAllTheFreeForAll Posts: 7Member

    : You have to normally rewrite entire text files because they do not have fixed-length records. You can't replace one word with another word of a different size without either leaving some extraneous characters when the new word is shorter than the original word, or without overwiritng parts of the following text when the new word is longer than the original word. When either of these two conditions exist, the only way to change the file is to rewrite the entire thing.
    :
    allright its plain and simple
    you obviously dont know what your doing
«1
Sign In or Register to comment.