Need help reading a file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amkcoder

    #1

    Need help reading a file.

    I am trying to read a file a character at a time,
    I want to read in the new line character.
    I am printing out each character to the screen.
    For some reason it will not print out the new line.
    I want to do this so the line will break on the console
    while reading it in a character at a time.
    I am using fgetc on a file opened for reading in text mode.
    What could be the problem?
    Can anyone help?...



  • Sjouke Burry

    #2
    Re: Need help reading a file.

    Amkcoder wrote:
    I am trying to read a file a character at a time,
    I want to read in the new line character.
    I am printing out each character to the screen.
    For some reason it will not print out the new line.
    I want to do this so the line will break on the console
    while reading it in a character at a time.
    I am using fgetc on a file opened for reading in text mode.
    What could be the problem?
    Can anyone help?...
    >
    >
    >
    If you want to see each character, read in binary mode, in text
    mode the cr/lf chars are translated(on MS dos/win).

    Comment

    • Keith Thompson

      #3
      Re: Need help reading a file.

      Amkcoder <frontal_lobe_d estroyer@yahoo. comwrites:
      I am trying to read a file a character at a time,
      I want to read in the new line character.
      I am printing out each character to the screen.
      For some reason it will not print out the new line.
      I want to do this so the line will break on the console
      while reading it in a character at a time.
      I am using fgetc on a file opened for reading in text mode.
      What could be the problem?
      Can anyone help?...
      Show us your code.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Barry Schwarz

        #4
        Re: Need help reading a file.

        On Fri, 3 Oct 2008 19:41:03 -0700 (PDT), Amkcoder
        <frontal_lobe_d estroyer@yahoo. comwrote:
        >I am trying to read a file a character at a time,
        >I want to read in the new line character.
        >I am printing out each character to the screen.
        >For some reason it will not print out the new line.
        >I want to do this so the line will break on the console
        >while reading it in a character at a time.
        >I am using fgetc on a file opened for reading in text mode.
        >What could be the problem?
        >Can anyone help?...
        Unless you have a syntax error on line 42 (in which case I will
        immediately patent my crystal ball), you will get more helpful answers
        by posting a small compilable program that exhibits the behavior in
        question.

        --
        Remove del for email

        Comment

        • Malcolm McLean

          #5
          Re: Need help reading a file.


          "Amkcoder" <frontal_lobe_d estroyer@yahoo. comwrote in message news:
          >I am trying to read a file a character at a time,
          I want to read in the new line character.
          I am printing out each character to the screen.
          For some reason it will not print out the new line.
          I want to do this so the line will break on the console
          while reading it in a character at a time.
          I am using fgetc on a file opened for reading in text mode.
          What could be the problem?
          Can anyone help?...
          >
          How are you printing to the screen?
          The standard C function putchar(), printf(), puts and the like will treat a
          newline as a line break, however that isn't true of all screen-drawing
          functions, nor is it true (usually) if you are accessing the character
          buffer directly.

          --
          Free games and programming goodies.


          Comment

          • pete

            #6
            Re: Need help reading a file.

            Amkcoder wrote:
            I am trying to read a file a character at a time,
            I want to read in the new line character.
            I am printing out each character to the screen.
            For some reason it will not print out the new line.
            I want to do this so the line will break on the console
            while reading it in a character at a time.
            I am using fgetc on a file opened for reading in text mode.
            What could be the problem?
            Can anyone help?...
            /* BEGIN type_2.c */

            #include <stdio.h>

            #define ARGV_0 "type_2"

            int main(int argc, char *argv[])
            {
            int rc;
            FILE *fp;

            if (argc 1) {
            while (*++argv != NULL) {
            fp = fopen(*argv, "r");
            if (fp != NULL) {
            while ((rc = fgetc(fp)) != EOF) {
            putchar(rc);
            }
            fclose(fp);
            } else {
            printf("\nfopen () problem with \"%s\"\n", *argv);
            break;
            }
            }
            } else {
            fputs("Usage:\n >" ARGV_0, stdout);
            puts(" <FILE_0.txt<FIL E_1.txt<FILE_2. txt...");
            }
            return 0;
            }

            /* END type_2.c */

            --
            pete

            Comment

            • osmium

              #7
              Re: Need help reading a file.

              "Amkcoder" wrote:
              >I am trying to read a file a character at a time,
              I want to read in the new line character.
              I am printing out each character to the screen.
              For some reason it will not print out the new line.
              I want to do this so the line will break on the console
              while reading it in a character at a time.
              I am using fgetc on a file opened for reading in text mode.
              What could be the problem?
              Can anyone help?...
              Maybe this link will help. Look especially at the section on
              Representations . You can ignore the stuff on Unicode. Also, search google
              groups for the hundreds of responses others who had the same problem have
              received over the years. In short, depending on your OS, there may be magic
              going on under the hood that you don't know about.




              Comment

              • Barry Schwarz

                #8
                Re: Need help reading a file.

                On Sat, 04 Oct 2008 08:08:28 -0400, pete <pfiland@mindsp ring.com>
                wrote:
                >Amkcoder wrote:
                >I am trying to read a file a character at a time,
                >I want to read in the new line character.
                >I am printing out each character to the screen.
                >For some reason it will not print out the new line.
                >I want to do this so the line will break on the console
                >while reading it in a character at a time.
                >I am using fgetc on a file opened for reading in text mode.
                >What could be the problem?
                >Can anyone help?...
                >
                >/* BEGIN type_2.c */
                >
                >#include <stdio.h>
                >
                >#define ARGV_0 "type_2"
                >
                >int main(int argc, char *argv[])
                >{
                int rc;
                FILE *fp;
                >
                if (argc 1) {
                while (*++argv != NULL) {
                You apparently intend to process all the files the user specifies when
                he invokes the program.
                fp = fopen(*argv, "r");
                if (fp != NULL) {
                Wouldn't it be a good idea to tell the user which file the following
                data came from?
                while ((rc = fgetc(fp)) != EOF) {
                putchar(rc);
                }
                fclose(fp);
                } else {
                printf("\nfopen () problem with \"%s\"\n", *argv);
                break;
                Why break? Just because one file could not be opened is no reason to
                skip processing any remaining files.
                }
                }
                } else {
                fputs("Usage:\n >" ARGV_0, stdout);
                puts(" <FILE_0.txt<FIL E_1.txt<FILE_2. txt...");
                }
                return 0;
                >}
                >
                >/* END type_2.c */
                --
                Remove del for email

                Comment

                • CBFalconer

                  #9
                  Re: Need help reading a file.

                  Amkcoder wrote:
                  >
                  I am trying to read a file a character at a time, I want to read
                  in the new line character. I am printing out each character to
                  the screen. For some reason it will not print out the new line.
                  I want to do this so the line will break on the console while
                  reading it in a character at a time. I am using fgetc on a file
                  opened for reading in text mode. What could be the problem?
                  Copying a text file is extremely simple, especially if you can
                  attach a file to stdin with a command such as:

                  $ copytext <filename.ext

                  A suitable program is:

                  #include <stdio.h>

                  int main(void) {
                  int ch;

                  while (EOF != (ch = getchar())) putchar(ch);
                  return 0;
                  }

                  Note you need not pay any attention to new lines.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.

                  Comment

                  • pete

                    #10
                    Re: Need help reading a file.

                    Barry Schwarz wrote:
                    On Sat, 04 Oct 2008 08:08:28 -0400, pete <pfiland@mindsp ring.com>
                    wrote:
                    >
                    >Amkcoder wrote:
                    >>I am trying to read a file a character at a time,
                    >>I want to read in the new line character.
                    >>I am printing out each character to the screen.
                    >>For some reason it will not print out the new line.
                    >>I want to do this so the line will break on the console
                    >>while reading it in a character at a time.
                    >>I am using fgetc on a file opened for reading in text mode.
                    >>What could be the problem?
                    >>Can anyone help?...
                    >/* BEGIN type_2.c */
                    >>
                    >#include <stdio.h>
                    >>
                    >#define ARGV_0 "type_2"
                    >>
                    >int main(int argc, char *argv[])
                    >{
                    > int rc;
                    > FILE *fp;
                    >>
                    > if (argc 1) {
                    > while (*++argv != NULL) {
                    >
                    You apparently intend to process all the files the user specifies when
                    he invokes the program.
                    >
                    > fp = fopen(*argv, "r");
                    > if (fp != NULL) {
                    >
                    Wouldn't it be a good idea to tell the user which file the following
                    data came from?
                    The program was written mostly to be easy to test,
                    and not so much for using.
                    >
                    > while ((rc = fgetc(fp)) != EOF) {
                    > putchar(rc);
                    > }
                    > fclose(fp);
                    > } else {
                    > printf("\nfopen () problem with \"%s\"\n", *argv);
                    > break;
                    >
                    Why break? Just because one file could not be opened is no reason to
                    skip processing any remaining files.
                    OK.
                    >
                    > }
                    > }
                    > } else {
                    > fputs("Usage:\n >" ARGV_0, stdout);
                    > puts(" <FILE_0.txt<FIL E_1.txt<FILE_2. txt...");
                    > }
                    > return 0;
                    >}
                    >>
                    >/* END type_2.c */
                    --
                    pete

                    Comment

                    Working...