An error appears when reading a file on getline in MS VS2010 an error appears on while( in.getline(buf, 1024) ){ when reading an empty line, namely when reading newline characters \r\n .

Please tell me what the jamb ....?
An example of lines from the file "input.txt" :

angliyskie.bukvi.cifri1="русские буквы цифры1";

\r\n (it breaks in this place. I don’t know how to arrange an empty line)

angliyskie.bukvi.cifri2="русские буквы цифры2";

 #include "stdafx.h" #include <iostream> #include <fstream> #include <sstream> #include <stdio.h> #include <string> std::ifstream in("input.txt"); std::ofstream out("output.txt"); char str1[80] = {0}; char str2[80] = {0}; int main() { char buf[1024]={0}; while( in.getline(buf, 1024) ){ string str(buf); if (sscanf(buf, "%[*az,*0-9,Az,., ]%*c%[^\n]s", &str1, str2)== 1){ if (str1[0]=='/'){ goto breakAll; } else if (str1[0]==' '){ goto breakAll; } else if (str1[0]==NULL){ goto breakAll; } else if (str1[0]=='\n'){ goto breakAll; } else if (str1[0]=='\r'){ goto breakAll; } else (out << str1<<endl); str1[0]=0; str2[0]=0; str=' ';} breakAll:; buf[0]=0;} out.close(); in.close(); } 

Closed due to the fact that off-topic participants are Harry , 0xdb , user192664, aleksandr barakin , user300000 11 Oct '18 at 9:24 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, let's say Pie
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, community spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Tell me, do you have a semicolon in &str1, str2)== 1);{ and in your working code? And - what is your understanding of "an error when reading a file"? Which one? - Harry
  • @Harry, "Unhandled exception in" 0x52e83933 (msvcp100d.dll) "in" test.exe ": 0xC0000005: Access violation while reading" 0xef20fcf6 "." Yes, the semicolon was superfluous, thanks! - IronZeshadow
  • @Harry, here's a picture I apologize for curve hosting with ads - IronZeshadow
  • What is %[^\n]s at the end of the scanf format? What is this s doing there? What is generally %[*az,*0-9,Az,., ] ? Where did you get such a strange format? - AnT
  • @AnT, %[^\n]s at the end of the scanf format means reading into an array of characters to the end of the line. The symbol s needed to determine the format of the string. Without s str2 variable will not be written. The strange format %[*az,*0-9,Az,., ] Does the same, namely it defines the format for the variable in which the characters in the range from A to Z, from a to z, from 0 to 9. Life made :) if it's too lazy to google, then here is the specification of the format scanf - IronZeshadow

1 answer 1

Firstly, why the hodgepodge from C I / O and classes of C ++ strings was needed is not clear. Anyway...

Secondly, some rubbish is written in sscanf

  1. The format %[*az,*0-9,Az,., ] - causes reasonable suspicions of nonsense. It looks like an attempt to read letters, numbers, space and point. Then the correct format will look like this.

     %[A-Za-z0-9. ] 

    Why you put commas there and what the sacral meaning of these * is not clear.

  2. What does some s at the end of the format string? This s is a completely separate, stand-alone character in the format string, which will cause sscanf to require each of your lines to end with s . Do you really need it?

    This is most like the occasional odd belief that the %[] format is actually written as %[]s , which, of course, is nonsense. It is especially strange to see it in your code, where after the first %[] you do not have any s , and after the second you suddenly decided to put it. What is this throwing?

  3. What a strange zoo from the arguments-receivers for sscanf

     , &str1, str2) 

    Why did this & ? Appear in the first argument?

  4. The result of sscanf compared to 1 . Why with 1 if actually two arguments are read. Did you want to require that the reading of the second argument be unsuccessful? Or something else?

  5. The variable buf declared locally in main , and all others are file-level. On the basis of what logic were decisions about where to declare variables?

  6. The string str2 is read, but not used anywhere in the code. Why did you then get the str2 buffer at all and read something in it? Your sscanf ( %*c format) shows that you know what role the * character plays in suppressing assignment. Why then did you not use it for the third format?

  7. if (str1[0]==NULL) is another incomprehensible comparison. str[0] is a char value. NULL intended for use with pointers. What did you want to say with this comparison?