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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:29:36+00:00 2026-06-15T02:29:36+00:00

I am writing using CodeBlocks, GCC and using C language. The code I have

  • 0

I am writing using CodeBlocks, GCC and using C language. The code I have is as follows:

char word[50];
FILE *fn;
fn = open("word.txt", "rb");
if(fn == NULL) perror("File not opened");
while(!feof(fn))
{
    fscanf(fn, "%s", word);
}

The text file has only one word in it (at present that is “batman”). I have tried various versions of this code including the following:

char* word;
char = malloc(50);
FILE *fn;
fn = open("word.txt", "rb");
if(fn == NULL) perror("File not opened");
while(!feof(fn))
{
    fscanf(fn, "%s", &word);
}

I honestly don’t know why (and why this should be SO difficult) but the error that CodeBlocks is giving me is the dreaded SIGSEV segmentation fault at the feof line. When I didn’t do the while loop the fscanf would throw a Segmentation fault. Please help! I don’t often work in C and I remember why now. Even the most simple of things can be SO difficult. Thanks in advance.

UPDATE:
I have actually got this to work (the top one) by changing the open line to:

fn = fopen("C://Temp//word.txt", "rb");

How can this work with relative paths?

  • 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-15T02:29:37+00:00Added an answer on June 15, 2026 at 2:29 am

    Change

    fn = open("word.txt", "rb");
    

    to

    fn = fopen("word.txt", "rb");
    

    open returns an int, which is not a FILE*. feof and fscanf expect a FILE* not an int as its first parameter, that’s the reason why feof() and, if you remove it, fscanf() segfaults.

    And as others already noted, change fscanf(fn, "%s", &word) to fscanf(fn, "%s", word).

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

Sidebar

Related Questions

I am writing ESME using logica smpp lib , but have a serious problem
I've been writing programs using OpenGL. Recently, I started learning OpenGL Shading Language. I'm
In a WPF app that I'm writing using the MVVM pattern, I have a
I'm using Code::Blocks on Ubuntu 10.10 (Maverick Meerkat). I have connected a Mac keyboard
I have been wondering what is better way of using code. Is there any
I have a game I'm writing using PHP. For the purposes of my question
I have a contact form that I'm writing using jQuery and PHP. Below is
I am writing using JavaScript. I have a PID of a process. How do
I have the following JS file, which contains a function I'm using to draw
I'm writing a C++ Package for later use using Code::Blocks. The project structure looks

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.