Thread: How to read only the first 5 Lines from a file and segmentation error 11

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    16

    How to read only the first 5 Lines from a file and segmentation error 11

    I have 2 questions. For some reason I am getting a segmentation error and I cant figure out why. I'm new to C programming so it may be obvious but I could not find anything in my program. Here is the code:

    Code:
    //
    //  main.c
    //  head
    //
    //  Created by Bryant Sahota on 7/1/18.
    //  Copyright © 2018 Bryant S. All rights reserved.
    //
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, const char * argv[]) {
        //If user doesnt specify file name
        if(argc == 1)
        {
            printf("head[OPTION][FILE]\n");
        }
        //Prints first 10 lines or all of them if fewer than 10
        else if(argc == 2)
        {
            char fileName[100];
            strcpy(fileName, argv[2]);
            FILE *fPtr;
            if ((fPtr = fopen(fileName, "r")) == NULL){
                printf("Error! opening file");
            }
            else
            {
                printf("in else");
                char fileLine[30];
                for(int i = 0; i < 10; i++)
                {
                    fgets(fileLine, 30, fPtr);
                }
                
            }
        }
    }
    Ignore what is in the nested (second) else statement. Thats my next question. Ive been looking online how to only print the first 5 Lines of a file (or all of them if less) but I cant find anything i can understand. Can anyone push me in the right direction?

    this is all that is in the file i'm trying to read from:
    hello
    world
    Red Dead Redemption
    cats and dogs
    I need to
    make the
    file a lot longer
    but now I’m done
    so
    I’m
    Gonna
    Close the file now.
    Last edited by KingKush; 07-02-2018 at 03:08 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main(int argc, char **argv) {
        if (argc != 2) {
            printf("Usage: myhead FILE\n");
            return EXIT_FAILURE;
        }
     
        FILE *fPtr = fopen(argv[1], "r");
        if (fPtr == NULL) {
            perror("Error opening file");
            return EXIT_FAILURE;
        }
     
        char line[500];
        for (int i = 0; i < 10; i++) {
            if (fgets(line, sizeof line, fPtr) == NULL)
                break;
            fputs(line, stdout);
        }
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    @john.c Thanks sooo much for your help! From your code i was able to decipher what went wrong with my code. When i was assigning the filename i was out of bounds in the argv array(I used argv[2] instead of argv[1]).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when I try to read in the lines of a file
    By Zsofia Komaromi in forum C Programming
    Replies: 1
    Last Post: 10-21-2013, 02:13 PM
  2. My program don't read all lines of a text file
    By Netcode in forum C Programming
    Replies: 5
    Last Post: 04-13-2012, 07:45 PM
  3. Read lines from a file
    By steffi in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 06:05 AM
  4. Best way to read lines out of a text file
    By movl0x1 in forum C Programming
    Replies: 9
    Last Post: 05-29-2007, 12:45 PM
  5. read multiple lines from a file
    By YankeePride13 in forum C Programming
    Replies: 2
    Last Post: 11-10-2005, 10:30 PM

Tags for this Thread