Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to remove the carriage return to newline conversion in C.

How to remove the carriage return to newline conversion in C.

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Razanust
    wrote on last edited by
    #1

    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

    K R 2 Replies Last reply
    0
    • R Razanust

      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

      K Offline
      K Offline
      khb
      wrote on last edited by
      #2

      Lines in files are terminated by "\r\n". You still seem to print out the "\n" after handling the "\r". Kind regards Marcus

      1 Reply Last reply
      0
      • R Razanust

        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

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups