PDA

Click to See Complete Forum and Search --> : string manipulation


hmm
Aug 5th, 2001, 01:14 AM
O.S: Win98
Compiler: Dev-C++ version 4
Level: keen newbie ( becoming frustrated)

Hello,

Working my way through Teach Yourself C++. One of the annoying things about the book is way the examples are line numbered. I’ve written console app that removes the numbers and the colons, but I would like to write a win app that does the same. I’ve done everything (with help from people here), but the string routine that removes the line numbers and colons.

I appreciate some pointers on how to achieve this


An example:

1: // Listing 8.2 Using pointers
2:
3: #include <iostream.h>
4:
5: typedef unsigned short int USHORT;
6: int main()
7: {
8: USHORT myAge; // a variable
9: USHORT * pAge = 0; // a pointer
10: myAge = 5;
11: cout << "myAge: " << myAge << "\n";

etc…


Thanks again

hmm :confused:

parksie
Aug 5th, 2001, 02:31 AM
If you've already written a console app to do it, why do you need to rewrite it for a Windows program? Just reuse your logic code :)

hmm
Aug 5th, 2001, 02:50 AM
O.S: Win98
Compiler: Dev-C++ version 4
Level: keen newbie ( becoming frustrated)

parksie,

Why windows? Well, one to start looking at win programming, two its easier to cut and paste between apps. The code logic using the stream functions ignore() and getline() to trim the string. I maybe misunderstanding the use of these functions, but they are not available to win apps (I hope I’m not being blindly stupid )?

#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
char sFileName[20];
char dFileName[20];
char Buffer[255];

cout << "Enter file name!\n:";
cin >> sFileName;
cout << "Enter destination file name!\n:";
cin >> dFileName;


ifstream fin(sFileName);
if (!fin)
{ cout << "Unable to open file!";
return (1);
}
ofstream fout(dFileName);
do
{
fin.ignore(5,':') ;
fin.getline(Buffer,255);
fout << Buffer << "\n" ;
}
while(!fin.eof());

fout.close();
fin.close();

cout << "File Created";
return 0;
}

thanks

hmm :)

parksie
Aug 5th, 2001, 03:24 AM
I wasn't questioning your wish to make it as a Windows program - I'm aware of the benefits of Windows programs ;) I was just pointing out that in many cases the logic is the same. In this case, have a look at the ostringstream and istringstream classes - they work fairly similarly to cout/cin, but they act on strings instead :)