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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:42:27+00:00 2026-06-09T17:42:27+00:00

I have written a C program to categorize some strings. I use FILE streams

  • 0

I have written a C program to categorize some strings. I use FILE streams to read and write files. But I found a problem. Usually it should categorize strings, but it doesn’t. I think this code is OK so I can’t find the problem. Please help me.

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

int main()
{
    char line[80]; //create a string
    FILE *r_in = fopen("spooky.csv", "r"); // this is the source file
    FILE *w_ufo = fopen("ufo.txt", "w"); // strings with "UFO" will be written to here
    FILE *w_disapp = fopen("disappearance.txt", "w"); // strings with "Disappearance" will be written to here  
    FILE *w_others = fopen("others.txt", "w"); // others will be written to here

    while (fscanf(r_in, "%79[\n]\n", line) == 1)
    {
        if(strstr(line, "UFO")) // I think here is the problem (with strstr())
            fprintf(w_ufo, "%s\n", line);
        else if(strstr(line, "Disappearance"))
            fprintf(w_disapp, "%s\n", line);
        else
            fprintf(w_others, "%s\n", line);
    }

    fclose(w_ufo);
    fclose(w_disapp);
    fclose(w_others);

    return 0;
}

The source file “spooky.csv”:

30.685163,-68.137207,Type=Yeti
28.304380,-74.575195,Type=UFO
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
30.496017,-73.333740,Type=Disappearance
26.224447,-71.477051,Type=UFO
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis

I think the problem is strstr(), please tell about the problem.

  • 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-09T17:42:29+00:00Added an answer on June 9, 2026 at 5:42 pm

    The problem is in the fscanf. You probably want:

    while (fscanf(r_in, "%79[^\n]\n", line) == 1)
                             ^
    

    Or just use fgets as Joachim Pileborg commented.

    while (fgets(line, sizeof(line), r_in))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program in Java to read in a text file of
I have written a program in clojure but some of the functions have no
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written my program in python. It is written across seven files and
I have written a java program generates lots of files (say txt files) in
I have written a program which takes a file as input and whenever it
I am new to Qt . I have written a program to read and
hey guys, beginner here. I have written a program that outputs files to .txt's
I have written a program that stores booking information in a text file in
I have written a program which scans css files using jar cssparser-0.9.5.jar and 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.