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

Thread: file i/o

  1. #1
    Join Date
    Jul 2013
    Posts
    161

    file i/o

    i want the maching to be inserting numbers itself from a name.txt file (no problem here)
    but lets say i have the numbers this way

    23 24 25 26
    27 28 28 30
    31 32 33 34

    in my name.txt file

    how do i tell the machine in c++/c++11 that...if you are on a new line, before reading the first digit do something? and if you are at the end of the line..before moving on to a new line do something
    if we take the numbers i have written as an example...
    before reading in 23 "do something" after reading last digit on this line..that is 26 "do something"
    if on a new line, that is before reading 27 do something..after reading last digit on this line..that is 30 "do something"
    if on new line, that is before reading 31.."do something" afte reading 34 do something...

    this is not the EOF thing...i hope i explained myself well..

    i will like to give thanks to the two best helpers on this forum 2KAUD and mckenzie...with 2kaud being the very best; straight to point no beating about the bush guy thanks guys... infact am looking forward to your responses

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

    Re: file i/o

    Something like this could be what you want
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    const string name = "name.txt";
    
    int main()
    {
    ifstream ifs(name.c_str());
    
    	if (!ifs.is_open()) {
    		cout << "Cannot open file" << endl;
    		return 1;
    	}
    
    string line;
    
    	while (getline(ifs, line)) {
    		cout << "Process at begin of line" << endl;
    
    		stringstream ss;
    		string elem;
    
    		ss.str(line);
    		while (ss >> elem)
    			cout << elem << endl;
    
    		cout << "Process at end of line" << endl;
    	}
    }
    From your data it produces output
    Code:
    Process at begin of line
    23
    24
    25
    26
    Process at end of line
    Process at begin of line
    27
    28
    28
    30
    Process at end of line
    Process at begin of line
    31
    32
    33
    34
    Process at end of line
    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
    Jul 2013
    Posts
    161

    Re: file i/o

    Hi 2kaud..thanks for the quick reply...
    but there are some stream objects I've never used before and reading about them on cplusplus.com has given me some idea but things are not just clear yet... can you please comment line by line what is happening...I hope I'm not asking too much...

    how we're using stringstream objects to input integers i still don't get it... hope you can help me with some little more explanation..thanks in advance

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: file i/o

    how we're using stringstream objects to input integers
    I'm reading each of the numbers as type string, but if you want them as type int
    change
    Code:
    string elem;
    to
    Code:
    int elem;
    Once the file is open successfully, the program loops reading each complete line until an error occurs when all lines that can be read have been read.

    Once each line has been read then perform 'begin line' processing.

    Create a string stream with the contents of the line that has been read.

    Then loop extracting each element from the string stream until an error occurs. When the error occurs then perform 'end line' processing.

    The >> is for the stream extraction. It will try to extract the next element from the stream if it is valid for the type specified for the extraction.

    >> works on the usual file streams, string streams etc to perform stream extraction.

    For further info about using stringstreams see
    http://www.learncpp.com/cpp-tutorial...s-for-strings/
    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)

  5. #5
    Join Date
    Jul 2013
    Posts
    161

    Re: file i/o

    thanks 2kaud the code works..and there was no need to change string elem to int elem...
    you could have told me sstreams are used to convert int to strings for input..but i read about it and i got it and didn't make any modifications
    but one last question...
    i was expecting the code to get only only one line..but fortunately for my convenience it gets the whole page till EOF..
    just for understanding...how is this possibile...just for my understanding..lets assume i want to get only the first line...where do i tweak the code???
    thanks in advance Master 2kaud..

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

    Re: file i/o

    sstreams are used to convert int to strings for input.
    sstreams are used to enable the standard stream operators to work with a stream originating from a string rather than a file. Once you have a stream then the same stream operators work. >> is the standard stream extraction operator which will work on any input stream - whether file stream or string stream etc. >> takes the next element from the stream and puts in the specified variable if >> can perform the necessary conversions.

    In this case its extracting from stream to string (or stream to int) but not int to string.

    I was expecting the code to get only only one line..but fortunately for my convenience it gets the whole page till EOF
    The outer while loop obtains a line from the file until an error occurs (which would usually be EOF). if you only want to process the first line then replace
    Code:
    while (getline(ifs, line))
    with
    Code:
    getline(ifs, line);
    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
    Jul 2013
    Posts
    161

    Re: file i/o

    ok ok loads of thanks for the clarification

  8. #8
    Join Date
    Jul 2013
    Posts
    161

    Re: file i/o

    // call to compare from function extract_pos_wins()

    compare_with_mapp (cont, prob_analysis_1);
    compare_with_mapp (cont, prob_analysis_2);
    void compare_with_mapp(std::vector<int>&, std::multimap<int,int> &mapp)
    {
    for (std::multimap<int,int>::iterator ptr2 = mapp.begin(); ptr2 != mapp.end(); )
    {
    int y = ptr2->second;
    cont2.push_back(ptr2->second);
    if (std::any_of (cont.begin(), cont.end(), [&ptr2](int i){return i == ptr2->second;}))
    { ptr2++;
    while(ptr2->second == y)
    { cont2.push_back(ptr2->second);
    ptr2++;
    }
    }
    if (sizeof(cont2) < 3 ) {cont2.clear();}
    else {values_to_play.push_back(0);
    values_to_play.push_back(0);
    values_to_play.push_back(0);
    for (int ptr = 0; ptr < cont2.size(); ptr++)
    {
    values_to_play.push_back(cont2[ptr]);
    }
    values_to_play.push_back(0);
    values_to_play.push_back(0);
    values_to_play.push_back(0);
    }

    }
    cont2.clear();
    }
    sir since you're online now..can you help me with this lil problem?

    am getting error undefined reference...


    /home/mike/Dropbox/C++/Rolling Real.o||In function `extract_pos_wins()':|
    Rolling Real.cpp|| undefined reference to `compare_with_mapp(std::vector<int, std::allocator<int> >, std::multimap<int, int, std::less<int>, std::allocator<std:air<int const, int> > >)'|
    Rolling Real.cpp|| undefined reference to `compare_with_mapp(std::vector<int, std::allocator<int> >, std::multimap<int, int, std::less<int>, std::allocator<std:air<int const, int> > >)'|
    ||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: file i/o

    When posting code, please use code tags rather than quote tags. Go Advanced, select the formatted code and click '#'.

    What are cont2 and values_to_play ? - they are used but not defined in the function.

    You are passing cont as a parameter to compare_with_map() but then directly trying to use cont in the function??

    Note that you can simplify
    Code:
    for (std::multimap<int,int>::iterator ptr2 = mapp.begin(); ptr2 != mapp.end(); )
    to
    Code:
    for (auto ptr2 = mapp.begin(); ptr2 != mapp.end(); )
    PS As this post is unrelated to the previous thread posts, it might have been better to start a new thread.
    Last edited by 2kaud; January 12th, 2015 at 11:53 AM.
    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)

  10. #10
    Join Date
    Jul 2013
    Posts
    161

    Re: file i/o

    ok will put code in code tags next time. sorry.
    std::vector<int>cont2;
    std::vector<int>values_to_play;

    are global variable declared up there somewhere....

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: file i/o

    Given the correct global variables, this code compiles OK for me with MS VS 2013.

    Code:
    #include <map>
    #include <vector>
    #include <algorithm>
    
    std::vector<int>cont2, cont;
    std::vector<int>values_to_play;
    
    void compare_with_mapp(std::vector<int>&, std::multimap<int, int> &mapp)
    {
    	for (std::multimap<int, int>::iterator ptr2 = mapp.begin(); ptr2 != mapp.end();)
    	{
    		int y = ptr2->second;
    		cont2.push_back(ptr2->second);
    		if (std::any_of(cont.begin(), cont.end(), [&ptr2](int i){return i == ptr2->second; }))
    		{
    			ptr2++;
    			while (ptr2->second == y)
    			{
    				cont2.push_back(ptr2->second);
    				ptr2++;
    			}
    		}
    		if (sizeof(cont2) < 3) { cont2.clear(); }
    		else {
    			values_to_play.push_back(0);
    			values_to_play.push_back(0);
    			values_to_play.push_back(0);
    			for (int ptr = 0; ptr < cont2.size(); ptr++)
    			{
    				values_to_play.push_back(cont2[ptr]);
    			}
    			values_to_play.push_back(0);
    			values_to_play.push_back(0);
    			values_to_play.push_back(0);
    		}
    
    	}
    	cont2.clear();
    }
    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)

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