How to remove the carriage return to newline conversion in C.
-
oBJECT: To copy the data stored in a file along with their respective line numbers. CODE: #include #include #include main() { int lno=1,n=0,x=1; char ch[80]; FILE*fp; fp=fopen("imp.txt","rb"); if (fp==NULL) {puts("Cannot open file"); exit(1);} printf("01 "); while(1) { ch[n]=fgetc(fp); n=n+1; if (ch[n-1]==EOF) break; else if (ch[n-1]!='\r') { printf("%c",ch[n-1]);} else if (ch[n-1]=='\r') {x=x+1; printf("\n0%d ",x);} } fclose(fp); } DATA STORED IN THE FILE: hello raza here how are you PROBLEM: Output is coming to be as following. 01 hello raza here 02 how are you It should be 01 hello raza here 02 how are you Now although i am reading the file in the binary mode, even then it is converting the carriage returns into new lines! Why? Also when i open the file in text mode the output is 01 hello raza here how are you
-
oBJECT: To copy the data stored in a file along with their respective line numbers. CODE: #include #include #include main() { int lno=1,n=0,x=1; char ch[80]; FILE*fp; fp=fopen("imp.txt","rb"); if (fp==NULL) {puts("Cannot open file"); exit(1);} printf("01 "); while(1) { ch[n]=fgetc(fp); n=n+1; if (ch[n-1]==EOF) break; else if (ch[n-1]!='\r') { printf("%c",ch[n-1]);} else if (ch[n-1]=='\r') {x=x+1; printf("\n0%d ",x);} } fclose(fp); } DATA STORED IN THE FILE: hello raza here how are you PROBLEM: Output is coming to be as following. 01 hello raza here 02 how are you It should be 01 hello raza here 02 how are you Now although i am reading the file in the binary mode, even then it is converting the carriage returns into new lines! Why? Also when i open the file in text mode the output is 01 hello raza here how are you
-
oBJECT: To copy the data stored in a file along with their respective line numbers. CODE: #include #include #include main() { int lno=1,n=0,x=1; char ch[80]; FILE*fp; fp=fopen("imp.txt","rb"); if (fp==NULL) {puts("Cannot open file"); exit(1);} printf("01 "); while(1) { ch[n]=fgetc(fp); n=n+1; if (ch[n-1]==EOF) break; else if (ch[n-1]!='\r') { printf("%c",ch[n-1]);} else if (ch[n-1]=='\r') {x=x+1; printf("\n0%d ",x);} } fclose(fp); } DATA STORED IN THE FILE: hello raza here how are you PROBLEM: Output is coming to be as following. 01 hello raza here 02 how are you It should be 01 hello raza here 02 how are you Now although i am reading the file in the binary mode, even then it is converting the carriage returns into new lines! Why? Also when i open the file in text mode the output is 01 hello raza here how are you
Razanust wrote:
It should be 01 hello raza here 02 how are you
int main()
{
int iLine = 0;
FILE* pFile = fopen("MyFile.txt", "r");
if(!pFile) return -1;printf("%.2d", ++iLine);
while(true)
{
char c = fgetc(pFile);
if(c==EOF) break;printf("%c", c); if(c=='\\n') printf("%.2d", ++iLine);
}
return 0;
}It is a crappy thing, but it's life -^ Carlo Pallini