Thread: A .txt file.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    A .txt file.

    How to open a .txt file in Ubuntu with C?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Quote Originally Posted by TheGreekMan2000 View Post
    How to open a .txt file in Ubuntu with C?
    Before you open a text file, or any other file, in C, you need to study a good book on the C Programming Language. You need a lot more knowledge about C so you know what to do after opening the file.

    You need to study a good book, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Only then you will be ready to begin programming in the C Language.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Unfortunately, I don't have much time due to everyday routine. So it would help me a lot if you helped me with this.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Quote Originally Posted by TheGreekMan2000 View Post
    Unfortunately, I don't have much time due to everyday routine. So it would help me a lot if you helped me with this.
    We all have a "everyday routine". If you cannot commit to learning the language from a qualified instructor or from an up-to-date book, then I cannot help you further. Without properly learning the language, as with any other skill, you will, and are in over your head.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Just in case this is still an unresolved question, I have pasted in a Windows C solution with helpful comments
    Code:
    /* 
    Preparation
    1) create a file: AccessThisFile.txt
    2) Enter the following text:
    "I have accessed this file
    And this is the second line of the file" */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
        FILE *FileP;  // A pointer used with files
        char FileContent[]=""; // Used to write the file content to a string
        char TheFileName[]="AccessThisFile.txt"; // the file is in the same folder as
                                                               // this .c file
    // Now open the file to read from it
        FileP = fopen(TheFileName,"r"); // r means to read from
        // or
        //FileP = fopen("AccessThisFile.txt","r"); // This will also work
    //
    // Now read from the file
       // Method 1
       // fscanf(FileP,"%s", FileContent); // will read in until the first space and stop
       // printf("\n%s", FileContent);      //  output: I
       // Method 2
       // fgets(FileContent, 70, (FILE*) FileP);    // will read in 70 characters, up to the
       // printf("\n%s", FileContent);    //  end of line.  Output: I have accessed this file
    //
       // Method 3, no need for char FileContent[]="";
        while( !feof(FileP) ) // go through the file one character at a time until you
        {                         // reach the end of the file, tested by feof
            char c = fgetc(FileP);    // c is one character, not previously declared
            printf("%c", c);            // as each character is read into c, print it to screen
        }
        fclose(FileP);  // now close the file
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > while( !feof(FileP) )
    See here as to why this is bad.
    Why it's bad to use feof() to control a loop - Cprogramming.com

    > char FileContent[]=""
    ...
    > // fgets(FileContent, 70, (FILE*) FileP); // will read in 70 characters
    1. The cast is pointless.
    2. Your buffer isn't at least 70 chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2017, 10:48 AM
  2. Replies: 6
    Last Post: 12-28-2015, 03:36 AM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM

Tags for this Thread