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 8240805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:37:00+00:00 2026-06-07T20:37:00+00:00

The three things mentioned in the title are somewhat new to me. I am

  • 0

The three things mentioned in the title are somewhat new to me. I am familiar with all of them conceptually but this is the first time I have tried to write my own program from scratch in C++ and it involves all three of these things. Here is the code:

int main(int argc, char *argv[])
{
FILE *dataFile;
char string [180];
dataFile = fopen(argv[1],"r");
fgets(string,180,dataFile);
fclose(dataFile);
}

It compiles fine, but when I execute using a simple input text file I get a segmentation fault. I have searched multiple tutorials and I can’t figure out why. Any help would be appreciated.

  • 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-06-07T20:37:01+00:00Added an answer on June 7, 2026 at 8:37 pm

    There are a few things you should be checking here. It still may not do what you expect, but it will avoid the errors you’re getting.

    int main(int argc, char** argv)
    {
      if(argc > 1) // FIRST make sure that there are arguments, otherwise no point continuing.
      { 
        FILE* dataFile = NULL; // Initialize your pointers as NULL.
        const unsigned int SIZE = 180; // Try and use constants for buffers. 
                                          Use this variable again later, and if 
                                          something changes - it's only in one place.
        char string[SIZE];
        dataFile = fopen(argv[1], "r");
        if(dataFile) // Make sure your file opened for reading.
        {
          fgets(string, SIZE, dataFile); // Process your file.
          fclose(dataFile); // Close your file.
        }
      }
    }
    

    Remember that after this, string may still be NULL. See ‘fgets’ for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many things that all programmers should know, but I am particularly interested
Ok, let me get some things out of the way first. I have OWNERSHIP
Warning : I have just picked up Maven, so things mentioned might be wrong
As mentioned in the Title , I have a Binary search tree. I want
I have browsed the forum and tried afew things mentioned in these links: Check
Ok my first post and i hope the title makes sense. I have an
Basically, I have an eclipse plug-in which, among other things, adds three tabs to
Can anybody tell about three things: How to retrieve TimeZone from java.util.Date instance? How
There are some things that I do a lot in a new project, for
I know how to use locks in my app, but there still few things

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.