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

  • Home
  • SEARCH
  • 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 8791751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:53:10+00:00 2026-06-13T22:53:10+00:00

I have difficulty using malloc and fscanf . I just want to read a

  • 0

I have difficulty using malloc and fscanf.
I just want to read a file and print out the result using
I got a segmentation fault error when I executed this code.
I am not sure what I have done wrong. I would be very grateful if someone points out a fix.
Here is my code:

#include <stdio.h> 
#include <stdlib.h>

int main(int argc, char* argv[]){
    char* buffer = (char*)malloc(*argv[1]); // Allocate memory 
    if(buffer=NULL) // if *argv[1] is not existed, exit the program    
        exit(1);

    int n = 0;
    FILE* fp=fopen("file.txt","r"); // Open the file
    do {
        buffer[n] =fscanf(fp,"%c",buffer); // read from file until it gets EOF
    } while(buffer[n] != EOF);
    fclose(fp); // Close the file

    printf("%s",buffer); // Print the stored string

    free(buffer); // Return the memory
    return 0;    
}
  • 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-13T22:53:10+00:00Added an answer on June 13, 2026 at 10:53 pm

    Got it. This:

    if(buffer=NULL)
    

    should be this:

    if(buffer==NULL)
    

    You’re setting buffer to NULL. I’m sure you can see what happens next.

    More generally, this code is trying to do several things, and it’s full of bugs. You should have tested the different functions separately and worked out those bugs along the way.

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

Sidebar

Related Questions

I have difficulty in using the vb code dom. Basically, I want to compile
I have some jQuery code that is having difficulty running because I'm using it
I am trying to build a football game using rails but have difficulty in
I have difficulty in understanding the use of union in C. I have read
I'm having difficulty using reinterpret_cast. Lets just say right off the bat that I'm
I am have difficulty setting a belongsTo relationship using ember data. I have a
I have difficulty in extracting data from datagrid. Since I was using selectionchange event
I'm having difficulty using two django crispy forms simultaneously. I have one form to
I have been using valgrind to debug my program (the program functions fine, just
all I have difficulty in linking the two activities by using two buttons. The

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.