Results 1 to 4 of 4

Thread: string manipulation

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Question string manipulation

    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

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width