Thread: removing/ignoring carrage returns from a file

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    removing/ignoring carrage returns from a file

    Hi all,

    I'm trying to pull data from a .dat file which is arranged like this:

    F 26 5
    F 64 4
    F 29 2
    M 12 3
    M 40 1

    Now I've got it pretty much working so far but the only issue I have is that there is a carrage return at the end of each line so when I output to the screen I get:

    F 26 5

    26 5
    F 64 4

    64 4
    F 29 2

    29 2
    M 12 3

    12 3
    M 40 1

    What I need to do is get the carrage return either not read or stripped from the output. so far my code looks like this:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    //void bar(int number);                             // Layout
    //void space(int number);                         // Layout
    
    void main()
    {
       FILE *rawdat;
       char sex;
       int age;
       int range;
    
    
       rawdat = fopen("H:\\sp3work\\SP3Project\\custsurvey.dat","r");
       if (rawdat == NULL)
       {
            printf("Cannot locate file");
       }
       else
       {
            while (fscanf(rawdat, "%c %d %d", &sex, &age, &range) != EOF)
                    printf("%c %d %d\n", sex, age, range);
       }
            if (fclose(rawdat) == EOF)
                    puts("Cannot close this file");
    
    printf("\n\n\nPress any key to continue");
       getch();
    }

    Any help would be greatly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can tell fscanf() to ignore newlines, but I believe it's easier just to add a char or digit to the statement, to "pull" the newline char off the input stream.

    Although conceptually in C, the \n is just one char, we know it's actually two, so experiment and see which way your compiler see's it, by adding one of these:

    ..."%c", newline);

    or

    %c %c", newline);

    or

    %d", newline);

    or

    ...%d %d", newline);

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    Thanks Adak,

    I altered the line:
    Code:
    while (fscanf(rawdat, "%c %d %d", &sex, &age, &range) != EOF)
    to:
    Code:
    while (fscanf(rawdat, "%c %d %d &c", &sex, &age, &range, &newline) != EOF)
    and it worked (I actually did try this solution earlier but think I forgot to put the comma between &range &newline and didn't look too hard into why I was getting an error....)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or simply add "fgetc(rawdat)" in the loop. Or fscanf("&#37;c%d%d\n", ...)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM