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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:51:14+00:00 2026-05-28T22:51:14+00:00

My file looks like: 123456789 My code gives me segmentation fault: #include <stdio.h> int

  • 0

My file looks like:

123456789

My code gives me segmentation fault:

#include <stdio.h>

int main(){

    FILE *f; 
    char ch[5];
    f = open("a.txt", "r");
    fgets( ch, 4, f); 
    ch[4] = NULL;
    printf("%s", ch); //Fixed
    return 0;
}

I am an absolute beginner. What am I doing wrong. My aim is to read first 4 characters of the file using fgets.

  • 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-28T22:51:15+00:00Added an answer on May 28, 2026 at 10:51 pm

    You’ll want to do

        printf("%s", ch);
    

    For the % format, the argument is a pointer to characters; by passing a single character by value, you’re telling printf to interpret that character’s ASCII value as a pointer, and that’s going to blow up on you; i.e., if the character is a 1, which is ASCII 49, then it’s going to look at byte 49 in memory for a string — and looking down there is generally verboten.

    But secondly, I see you’re calling open() instead of fopen(). You must use fopen() or you won’t get a FILE* as you’re expecting.

    Both of these individually would likely cause a segfault — you’ll need to fix them both.

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

Sidebar

Related Questions

The file looks like this: 14.809034;56.876646;Vaxjo; 15.809035;57.906646;Vaxjo2; 16.809035;58.906646;Vaxjo3; and the code is here so
My file looks like this: <MAIN> <SUB_MAIN>one</SUB_MAIN> <VER>version#</VER> (OTHER STUFF...) <LOCATION>PATH</LOCATION> </MAIN> <MAIN> <SUB_MAIN>two</SUB_MAIN>
My source file looks like this: <stuff> <s> <contents> <code>503886</code> <code>602806</code> </contents> ... </s>
part of my gitignore file looks like this: phoenix/config/ProjectConfiguration.class.php StatsFile.txt phoenix/StatsFile.txt phoenix/config/* phoenix/config/propel.ini phoenix/data/*
My source file looks like this: <x> <names> <name>name1</name> <name>name2</name> </names> <codes> <code>code1</code> <code>code2</code>
I encountered the following code in a file (looks like header file) iomanip of
My current .htacces file looks like this: DirectoryIndex index.php RewriteEngine on RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
The file looks like: a1,b1 a2,b2 ... I know the value a2. How to
The typical ConfigParser generated file looks like: [Section] bar=foo [Section 2] bar2= baz Now,
My config file looks like this: title = myTitle; otherTitle = myOtherTitle; when I

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.