Thread: Read txt file and print contents numbering each new line of text

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    8

    Read txt file and print contents numbering each new line of text

    Alright, I have written the following code but i am stuck. Here is the problem and below is the code ive written thus far:

    Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    
    
    int main()
    {
        char line[81], filename[21], c;
        int i = 1;
        FILE *inFile;
        
        printf("\nPlease enter the name of a file: ");
        gets(filename);
        inFile = fopen(filename, "a+");
        if (inFile == NULL)
        {
            printf("\nThe file %s was not successfully opened.", filename);
            printf("\nPlease make sure the specified file exists.");
            getch();
            exit(1);
        }
    
    
        while (fgets(line, 256, inFile) != NULL)
            printf("\n%s", line);
        rewind(inFile);
        printf("\nFile contents all uppercase: \n");
        while ((c = fgetc(inFile)) != EOF) { 
                printf("\n%d", i)
                putchar(toupper(c));    
                i++;
        }
        
        fclose(inFile);
        getch();
        return 0;
        
    }
    Thanks for the help guys.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Sorry i didnt change the (filename, "a+"); to (filename, "r"); before submission.

    My bad

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    2
    Also cross posted here.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    358
    So it's nearly there.

    The problem is you're printing out i for each character.

    Probably it's best to read the line with fgets(), then write a little function to change the whole string to upper case, then add the line numbers when you print out.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. read contents of a text file into a struct
    By lemonwaffles in forum C++ Programming
    Replies: 2
    Last Post: 08-03-2009, 02:20 PM
  4. Read and display contents from text file
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-03-2009, 03:25 PM
  5. trying to print a line from a text file
    By kryonik in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2006, 09:14 PM