C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with Edit File Posted by TheFreeForAll on 31 May 2006 at 2:17 PM
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<iostream.h>
#include<lvp\string.h>
#include<fstream.h>
#include<stdio.h>

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<LineNum;i++)
{
charcount+=S.length();
charcount+=2;
}
InFile.close();
EditFile.open(FileName.c_str(),ios::in|ios::out);
cout<<"Enter new data:";
cin>>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();
}
Report
Re: Help with Edit File Posted by stober on 31 May 2006 at 3:42 PM
edit your post to use code tags -- nobody wants to read all that unformatted code
Report
Re: Help with Edit File Posted by TheFreeForAll on 31 May 2006 at 3:47 PM
: edit your post to use code tags -- nobody wants to read all that unformatted code
:
what is a code tag?
Report
Re: Help with Edit File Posted by stober on 31 May 2006 at 4:22 PM
: : edit your post to use code tags -- nobody wants to read all that unformatted code
: :
: what is a code tag?
:

see the section named Outputting code or fixed width data

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

Report
Re: Help with Edit File Posted by TheFreeForAll on 31 May 2006 at 4:30 PM
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<iostream.h> 
#include<lvp\string.h> 
#include<fstream.h> 
#include<stdio.h> 

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<LineNum;i++) 
{ 
charcount+=S.length(); 
charcount+=2; 
} 
InFile.close(); 
EditFile.open(FileName.c_str(),ios::in|ios::out); 
cout<<"Enter new data:"; 
cin>>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(); 
}


Report
Re: Help with Edit File Posted by stober on 31 May 2006 at 4:46 PM
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?
Report
Re: Help with Edit File Posted by TheFreeForAll on 31 May 2006 at 5:06 PM
: 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<LineNum;i++)
{
charcount+=S.length();
charcount+=2;
}
InFile.close();
EditFile.open(FileName.c_str(),ios::in|ios::out);
cout<<"Enter new data:";
cin>>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]

Report
Re: Help with Edit File Posted by stober on 31 May 2006 at 8:43 PM
This message was edited by stober at 2006-5-31 20:44:45

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.

#include <sstring>
...
...
void EditFile()
{
    String FileName;
    String S;
    int LineNum;
    long newdata;

    vector<String> 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 << "\n";
    }
    out.close(); 




Report
Re: Help with Edit File Posted by TheFreeForAll on 2 Jun 2006 at 12:02 PM
is there any way you could try to fix edit file without changing the entire thing and base it off how it already is
Report
Re: Help with Edit File Posted by stober on 2 Jun 2006 at 5:07 PM
: 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.
Report
Re: Help with Edit File Posted by TheFreeForAll on 3 Jun 2006 at 9:55 AM

: 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
Report
Re: Help with Edit File Posted by stober on 3 Jun 2006 at 4:55 PM
This message was edited by stober at 2006-6-3 17:24:16

: :
: allright its plain and simple
: you obviously dont know what your doing
:


Ok -- I've only been doing this for about 20 years, so I guess I don't know what I'm talking about If you want to replace old text with new text of same length, then yes there are ways to do it without rewriting the whole file. How ? I guess you'l have to just figure it out yourself.







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.