Hey,
I was wondering how do you actually open and use the information already in a file once you have brought it into the program.
I need to use the information in it and then change it but I cannot figure out how to do this?
Thanks
Printable View
Hey,
I was wondering how do you actually open and use the information already in a file once you have brought it into the program.
I need to use the information in it and then change it but I cannot figure out how to do this?
Thanks
Open it up, read it into a string buffer, edit to your hearts content, write it back to the file.
Code:int main() {
std::ifstream inFile;
std::ofstream outFile;
std::string fileBuffer = "",
lineBuffer = "";
inFile.open("myfile.txt");
while(getline(inFile, lineBuffer, '\n'))
fileBuffer += lineBuffer + '\n'; // Don't forget to add the delimeter
inFile.close();
/* Using an assortment of std::string::find(), std::string::substr(),
std::string::replace(), etc... you edit your string here */
outFile.open("myfile.txt", std::ios::trunc);
outFile << fileBuffer;
outFile.close();
return 0;
}
Thankyou so much for that but what is a string buffer?
Added code to an edit.
Ok so say I had a file that had "hello" written in it and I needed to turn "hello" into ASCII values and then show these in the output (only the numbers not the original file material) how would I do that?
I have already done everything to get the file and open it I just can't figure out how to do the rest.
I actually need to add all the values together and output the result
Code:std::string word = "hello";
int asciiWord[word.size()];
for(int i = 0; i < word.size(); i++)
asciiWord[i] = (int)word[i];
for(int i = 0; i < word.size(); i++)
std::cout << asciiWord[i] << ' ';
So if "hello" was in a file called file1.txt which is the part where the information in the file is used?
I think your milk has spoiled. ;)Quote:
Originally Posted by SlyMaelstrom
Ok I don't understand it
Thanks anyway
I actually thought it wouldn't compile with the potentially variable integer, but it did fine without warning. I supposed there may be some compilers that would have a problem with it.Quote:
Originally Posted by hk_mp5kpdw
>> I have already done everything to get the file and open it I just can't figure out how to do the rest.
Show us that code, whatever you already have. It will make it easier to understand what the next step is that you are having trouble with.
using namespace std;
int length_of_syracuse(int n);
bool isPrime(int a);
int largest_prime_factor(int a);
int main()
{
int sumofints;
ifstream inFile;
char filename[20];
cout << "Enter the name of the input file: "<<flush;
cin >> filename;
inFile.open(filename);
So the file is open but i can't figure out how to use the information in it.
I explained what to do in my first post. Look at the code.
Just like you do:
...to read data that the user types in the console into a variable, you can do:Code:cin >> filename;
...to read from your open file into a variable.Code:int var;
inFile >> var;