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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:48:47+00:00 2026-05-26T15:48:47+00:00

My code works fine in a way. I have two issues with it though.

  • 0

My code works fine in a way. I have two issues with it though. One, at the end of printing the files to the standard output, it gives me a segmentation fault.

Two, I can’t use fputs to print out the data because I get a segmentation fault right away. So to fix it I use puts which prints it fine, but adds a ‘\n’ after every line making the text single line spaced as well as the seg fault at the end.

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

void concat(char *arg){

    char string[256];
    FILE *fp = fopen(arg, "r");

    while(!feof(fp)){
        fgets(string, 256, fp);
        //fputs(string, fp);
        puts(string);
    }

    fclose(fp);

}

void stdincat(){

    char string[256];   
    while(!feof(stdin)){
        fgets(string, 256, stdin);
        fputs(string, stdout);
    }
}

int main(int argc, char *argv[]){

    char argvalues[256][40];

    if(argv[1] == NULL)
        stdincat();
    else if(argv[1] != NULL){
        int i;

        for(i=1;i<=(argc);i++){
            concat(argv[i]);
        }
    }

    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-05-26T15:48:48+00:00Added an answer on May 26, 2026 at 3:48 pm

    The call to fputs you have commented out in concat is attempting to write to fp, which you opened only for reading, so it’s no surprise that it won’t/doesn’t work.

    Other than that, your reading loops: while(!feof(fp)) { (and similar except from stdin instead of fp) follow a familiar, widespread anti-pattern — loops like this don’t/won’t/can’t work correctly. You normally want to read and test for success in the same operation:

    while(fgets(string, 256, stdin))
        fputs(string, stdout);
    

    Edit: I should also mention that I would prefer to avoid the duplication of code in concat and stdincat. I’d rather pass the FILE * to read from as the parameter, so you’d use the same code to read from stdin or from other arbitrary files:

    // Warning: untested code.
    // error checking omitted for readability.
    void catfile(FILE *file) { 
        char line[256];
        while (fgets(line, sizeof(line), file))
            puts(line);
    }
    
    int main(int argc, char **argv) { 
        int i;
        if (argc == 1)
            catfile(stdin);
        else for (int i=1; i<argc; i++) {
            FILE *infile = fopen(argv[i], "r");
            catfile(infile);
            fclose(infile);
        }
        return 0;
    }
    

    Finally, I’d note that if you’re just going to copy an entire file, fgets probably is not the most efficient way to do the job. fread may be more suitable. While you’re at it, you might as well use binary mode reading and writing as well.

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

Sidebar

Related Questions

I'm having some issues with a jQuery AJAX call. My code works fine when
I have a block of code that generally works fine except when big lumps
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
this code works fine until I start scrolling: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This code works fine to find an available room within certain date, but it
This code works fine in C#: Expression.Lambda(LambdaBody); But none of the methods for building
The following code works fine: person = {:a=>:A, :b=>:B, :c=>:C} berson = {:a=>:A1, :b=>:B1,
My below code works fine and is used to populate a <select> item with
The following code works fine for Visual C++ 2008. However, when comes to Visual
The following code works fine #define open { #define close } #include<stdio.h> #define int

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.