Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:29:52+00:00 2026-05-29T17:29:52+00:00

I have been working on a small exercise for my CIS class and am

  • 0

I have been working on a small exercise for my CIS class and am very confused by the methods C uses to read from a file. All that I really need to do is read through a file line by line and use the information gathered from each line to do a few manipulations. I tried using the getline method and others with no luck.
My code is currently as follows:

int main(char *argc, char* argv[]){
      const char *filename = argv[0];
      FILE *file = fopen(filename, "r");
      char *line = NULL;

      while(!feof(file)){
        sscanf(line, filename, "%s");
        printf("%s\n", line);
      }
    return 1;
}

Right now I am getting a seg fault with the sscanf method and I am not sure why. I am a total C noob and just wondering if there was some big picture thing that I was missing.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:29:52+00:00Added an answer on May 29, 2026 at 5:29 pm

    So many problems in so few lines. I probably forget some:

    • argv[0] is the program name, not the first argument;
    • if you want to read in a variable, you have to allocate its memory
    • one never loops on feof, one loops on an IO function until it fails, feof then serves to determinate the reason of failure,
    • sscanf is there to parse a line, if you want to parse a file, use fscanf,
    • “%s” will stop at the first space as a format for the ?scanf family
    • to read a line, the standard function is fgets,
    • returning 1 from main means failure

    So

    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
        char const* const fileName = argv[1]; /* should check that argc > 1 */
        FILE* file = fopen(fileName, "r"); /* should check the result */
        char line[256];
    
        while (fgets(line, sizeof(line), file)) {
            /* note that fgets don't strip the terminating \n, checking its
               presence would allow to handle lines longer that sizeof(line) */
            printf("%s", line); 
        }
        /* may check feof here to make a difference between eof and io failure -- network
           timeout for instance */
    
        fclose(file);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a small Plagiarism detection engine which uses Idea from
I have been working on a small file manager module in a project where
I have been working on this, very, small photo cms lately. And now i
I have been working through a very small-scale WPF project in order to familiarize
I have been working on a small php app (400K total). But in the
I have been working with a string[] array in C# that gets returned from
I have been working on some legacy C++ code that uses variable length structures
I've been working on a small blog form but have been having trouble with
Ok so, I have been working on a small webapp in sinatra. After taking
So I have been working on getting a small (> 1 MB) video to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.