CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2013
    Posts
    161

    [RESOLVED] writing to file and newline( endl and '/n'

    am having problem asking for a new line when writing to a txt file...not working with std::endl and not with '\n'...i COMMENTED IN THE CODE BELOW WHERE THE PROBLEM ARISES....
    HTML Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    
    int main (){
    std::vector<std::vector<int> > test = {{1,2,3,0,3},
                                          { 0,1,3,0,0},
                                          {0,0,0,0,0},
                                          {1,2,3,0,3},
                                         {0,3,4,4,4},};
    /*
    for (int i = 0; i< 5; i++)
    for (int k = 0; k <3; k++)
        std::cout<<test[i][k];
    std::cout<<std::endl;
    std::cout<<test[4][3]; */
    
    std::vector<int> f = {1,2,3,4,5,6,7,8,9,10};
    int n = 0;
    for (int i = 0; i < f.size(); i++)
    {
         if (n <= 2)
         {
             std::cout<<f[i]<<", "; n++; 
         }
         else { n= 1; std::cout<<std::endl<<std::endl<<f[i]<<", ";
                           }
    }
    
    std::fstream file;
    	file.open("oppose.txt", std::ios::in | std::ios::out | std::ios::trunc);
    	if (file.is_open()){
    
            int k = 0;
    for (int i = 0; i < f.size(); i++)
    {
         if (k < 70)
         {
             file<<f[i]<<"    "<<std::endl; k++;// ASKING FOR NEWLINE HERE GIVES GOOD RESULT
         }
         else { k=1;
     file<<'n'<<'\n'<<'\n'<<f[i]<<" ,"; //ALL THE NEWLINES AM ASKING HERE ARE IGNORED! WHY?
                           }
    }
    	std::cout<<"writing to file successful!";
    	}
    
    
    	else
            {std::cout<<"could not write to file...";}
    
    return 0;
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: writing to file and newline( endl and '/n'

    Have you verified by debugging that the line in question gets executed at all? Regarding the code as you posted it, I'd take any bet that it does not...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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

    Re: writing to file and newline( endl and '/n'

    Please use code tags for c++ code and not HTML tags. Go advanced, select code and click '#'

    Have you used the debugger to see if the else part of the if statement is actually being executed? Does k ever get greater than 70?
    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)

  4. #4
    Join Date
    Jul 2013
    Posts
    161

    Re: [RESOLVED] writing to file and newline( endl and '/n'

    I notice the k<70 error...am gonna try it again..thanks

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

    Re: [RESOLVED] writing to file and newline( endl and '/n'

    I'm not sure what you are trying to accomplish with

    Code:
    int k = 0;
         for (int i = 0; i < f.size(); i++) {
              if (k < 70) {
                 file << f[i] << "    " << std::endl;
                 k++;// ASKING FOR NEWLINE HERE GIVES GOOD RESULT
             } else { 
                 k=1;
                 file << 'n' << '\n' << '\n' << f[i] << " ,";
                //ALL THE NEWLINES AM ASKING HERE ARE IGNORED! WHY?
            }
        }
    but it can be simplied to

    Code:
    for (int i = 0; i < f.size(); i++) {
         if ((i + 1) % 70) {
            file << f[i] << std::endl;
         } else {
            file << "\n\n" << f[i] << " ,";
         }
    }
    Last edited by 2kaud; August 5th, 2013 at 05:20 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)

  6. #6
    Join Date
    Jul 2013
    Posts
    161

    Re: [RESOLVED] writing to file and newline( endl and '/n'

    HI 2kaud...good evening master...please...i don't get what you mean by if(i+1)%70....in words...if i plus one divided by 70 gives no remainder??? is that what you mean?

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

    Re: [RESOLVED] writing to file and newline( endl and '/n'

    % is the mod operator. It returns the remainder for integer numbers. So 3 % 70 returns 3. 71 % 70 returns 1, 0 % 70 returns 0 and 70 % 70 returns 0. So i starts at 0. So when i is 0 to 68, i + 1 is 1 to 69 and 1 to 69 mod 70 gives a non-zero result so the if condition will be true. When i is 69, i + 1 is 70 and 70 % 70 gives 0 so the if condition is false and the else statement is executed. When i is 70, i + 1 is 71 and 71 % 70 is 1 which is positive so the cycle repeats until i is 139 (139 +1 is 140 and 140 % 70 is 0).

    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
    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