|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
(C++)Using strings to replace characters [fstream]
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
AMD Overclocker
|
Hey guys,
__________________I am struggling with a part of file reading where the goal is to "clean up" the code. Basically, I've gotten to the point where it reads each character and gives spaces between them. However, I need to replace the "text" part of the string with space. For instance, here is an example I'll use: Code:
12 Hello 34 OCN 56 I need your help Code:
12 34 56 All I would like is an explanation of HOW THIS CAN BE DONE...+1 rep to all who help, as usual. Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
ifstream lane9;
ofstream mglane9;
const int MAX = 1000;
char scores[MAX];
int count = 0;
lane9.open("lane9.dat");
if(lane9.fail())
{
cout << "ERROR! ";
exit(1);
}
mglane9.open("MGlane9.txt");
if(mglane9.fail())
{
cout << "ERROR OUTPUT";
}
while(!lane9.eof())
{
lane9 >> str;
cout << str << " ";
mglane9 << str << " ";
if(isdigit(count))
{
mglane9 << str << " ";
count++;
}
}
lane9.close();
mglane9.close();
getchar();
return 0;
}
|
|||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Intel Overclocker
|
Maybe not the best solution, but maybe keep track of the last character you encountered so that you know what should be done next... I.e. If the last character was a digit and you have a space, do something. Move space to previous character...
Hope you see where I am going with that.
__________________
Spelling mistakes in my post? I am probably typing on my iPod... Please forgive me! Columbus, OH, area pc repair!
|
|||||||||||||
|
|
|
|
|
#3 (permalink) |
|
Case Modder
![]() |
You'll need to loop over all of the characters in 'str', calling isdigit(str[i]) and if it is a digit, add that character to another string (or just write the digit to the output file). Oh, and you'll need to add a space between numbers.
__________________
Rich Custom Wooden Case Builder
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|