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(); }
&str1, str2)== 1);{and in your working code? And - what is your understanding of "an error when reading a file"? Which one? - Harry%[^\n]sat the end of thescanfformat? What is thissdoing there? What is generally%[*az,*0-9,Az,., ]? Where did you get such a strange format? - AnT%[^\n]sat the end of thescanfformat means reading into an array of characters to the end of the line. The symbolsneeded to determine the format of the string. Withoutsstr2variable 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