CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: help me)

  1. #1
    Join Date
    Sep 2013
    Posts
    3

    help me)

    How to write a programme that take strings frim file and push it into the stack with number of each sting string before it and later clean the stack?
    help

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: help me)

    How to write a programme
    Design the program, code the program from the design, test and debug the program and document the program.

    What is your c++ question? No one here is going to write an assignment program for you.

    See http://forums.codeguru.com/showthrea...ork-assignment
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2013
    Posts
    3

    Re: help me)

    #include <iostream>
    #include <stack>
    #include <string>

    int main ()
    {
    std::stack<std::string> mystack;
    FILE * file;
    file = fopen ("D:\\text.txt","r");
    char PrevStr[80];
    char NextStr[90];
    int a= 1;

    while (!feof(file))
    {
    fscanf (file, "%s", PrevStr);
    mystack.push(NextStr);
    sprintf(NextStr, "%d: %s", a, PrevStr);
    mystack.push(NextStr);
    a++;
    }

    while (!mystack.empty())
    {
    printf(mystack.top().c_str());
    mystack.pop();
    printf("\n");
    fclose(file);
    getchar();
    }
    }

  4. #4
    Join Date
    Sep 2013
    Posts
    3

    Re: help me)

    there is sth wrong..I can not find what..

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: help me)

    Read the file with fgets. Close the file only once, and do it out of while loop.

    Oh, and dont push next string twice.
    Last edited by Igor Vartanov; September 29th, 2013 at 03:17 PM.
    Best regards,
    Igor

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: help me)

    When posting code, please format the code first and use code tags. Go advanced, select the code and click '#'.

    As you are writing c++ code, why use 'c' style file i/o and not c++ file streams? Why use printf instead of cout?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: help me)

    Quote Originally Posted by Ksiuha View Post
    there is sth wrong..I can not find what..
    You used std::stack<std::string>, therefore I am assuming the other parts of the code should follow a "C++" design and not 'C' design.

    First, none of your code is specific to Visual C++ -- it is a standard C++ program. Next time, post in the Non-Visual C++ forum.

    Second use code tags when posting code.

    Third, you should be using streams, not fprintf() and printf().

    Fourth, use stringstreams or other C++ facility to concatenate a number to a string, not sprintf(). Your code will have a buffer overrun if the string is close to 80 characters, as this is the limit you placed on that buffer. If I gave you a text file with a line that is greater than 80 characters, your code may crash, or at least, overwrite the buffer.

    If you are not familiar with using string streams, here is an example:
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string s = "abc";
        int num = 123;
    
        // Now concatenate num onto s
        ostringstream strm;
        strm << num << ": " << s;  // this does the concatenation using operator <<
    
        // return the contents as a std::string
        string totalString = strm.str();
    
        // output the results
        cout << totalString;
    }
    If you run this program, you will see that totalString contains the concatenated 123 followed by a colon, followed by the value of s (which is "abc"). Note that there are no char buffers or sprintf() to do the concatenation, therefore it doesn't matter if the string s is 80 characters, 90 characters, or a million characters.

    The ostringstream maintains a dynamic buffer, therefore grows accordingly to what you place in it. With sprintf(), your buffer is limited to x characters, and there is no internal check to see if the string you place in the sprintf() buffer exceeds x characters, therefore exposing you to buffer overwrite errors.

    Last, when you say that "something is wrong", you should let us know what is wrong. You know exactly what is wrong -- what you may not know is how to fix the problem. However, at least tell us what is wrong with the program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 29th, 2013 at 09:48 PM.

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

Featured