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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:49:46+00:00 2026-05-27T17:49:46+00:00

A couple of questions really about the code below from which I gained assistance

  • 0

A couple of questions really about the code below from which I gained assistance in a previous post.

1). Any ideas why at the end of the ouput, I get a random garbage character printed? I am freeing the files etc and checking for EOF.

2). The idea is that it can work with multiple file arguements, so I want to create new file names which increment, i.e. out[i].txt, is that possible in C?

The code itself takes a file containing words all separated by spaces, like a book for example, then loops through, and replaces each space with a \n so that it forms a list, please find the code below:

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

/*
 * 
 */
int main(int argc, char** argv) {

FILE *fpIn, *fpOut;
int i;
char c;
while(argc--) {
    for(i = 1; i <= argc; i++) {
        fpIn = fopen(argv[i], "rb");
        fpOut= fopen("tmp.out", "wb");
        while (c != EOF) {
            c = fgetc(fpIn);
            if (isspace(c)) 
                c = '\n';
            fputc(c, fpOut );
        }
    }
}
fclose(fpIn);
fclose(fpOut);
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-27T17:49:47+00:00Added an answer on May 27, 2026 at 5:49 pm

    When you reach the end of file, you don’t break the loop. So you are calling fputc(c, fpOut); with c==EOF which is probably an undefined behavior, or at least the writing of a \0xff byte.

    And you don’t call fclose inside your while(argc--) loop, so your files (except the last) are mostly never closed nor flushed.

    At last, you don’t test the result of fopen and you should test that it is non null (and print an error message, perhaps with something about strerror(errno) or perror, in that case).

    You should have found out with a debugger (like gdb on Linux), and perhaps with the help of compiler warnings (but gcc-4.6 -Wall did not caught any bugs on your example).

    You could decide that the output file name is related to input file name, perhaps with

    char outname[512];
    for(i = 1; i < argc; i++) {
       fpIn = fopen(argv[i], "rb");
       if (!fpIn) { perror (argv[i]); exit(1); };
       memset (outname, 0, sizeof (outname));
       snprintf (outname, sizeof(outname)-1, "%s~%d.out", argv[i], i);
       fpOut= fopen(outname, "wb");
       if (!fpOut) { perror (outname); exit(1); };
       /// etc...
       fclose(fpIn);
       fclose(fpOut);
       fpIn = fpOut = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen a couple questions about how to convert a PFX to a cert
I have a couple questions about exceptions. 1) when you hit a catch block,
I have a couple questions about submitting blue-tooth enabled apps on the iPhone. I
I have a couple questions about creating a object (2 values) and how to
a couple of questions (never done javascript before). Below is part of the what
I have a couple questions about SSL certificates. I never used them before but
Update : I have a couple of what are probably silly questions about commenter
I have got couple questions about changing hibernate configuration. There is a project say
Hello I have a couple questions about PHP exec() and passthru(). 1) I never
So I have a couple of questions about fork(): 1) I'm pretty sure that

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.