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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:11:49+00:00 2026-05-29T15:11:49+00:00

EDIT: void print(const int *v, const int size) { FILE *fpIn; fpIn = fopen(char-array.txt,

  • 0
EDIT: 

void print(const int *v, const int size) {
 FILE *fpIn;
 fpIn = fopen("char-array.txt", "a");
 int i;  
 if (v != 0) {
   for (i = 0; i < size; i++) {
     printf("%d", (int)v[i]);
     fprintf(fpIn, "%d\n", (int)v[i]);   
   }
   perm_count++;
   printf("\n");
 }
 fclose(fpIn);
} 

I guess this is a relatively simple question 🙂

Basically the program is using a permutation algorithm, and printing the output to standard output in the console. I also want to write the content to a file via fprintf I assume. Though I cant seem to get it working. It just prints garbage characters into the first line in the text file and nothing more !

I will paste the code below, and help is much appreciated ! The write to file code is found within the print function.

Thanks,

T.

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

#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define PRINTTIME printf("%2.3f seconds used by the processor.", ((double)stopm-          startm)/CLOCKS_PER_SEC);

int perm_count = 0; 

void print(const int *v, const int size) {
  FILE *fpIn;
  fpIn = fopen("char-array.txt", "wb");
  int i;  
  if (v != 0) {
    for (i = 0; i < size; i++) {
      printf("%d", (char)v[i]);
      fprintf(fpIn, "%d", v[i]);  
      fprintf(fpIn, "\n");  
    }
    perm_count++;
    printf("\n");
  }
} 


void permute(int *v, const int start, const int n) {  
  int i;  
  if (start == n-1) {
    print(v, n);
  }
  else {
    for (i = start; i < n; i++) {
      int tmp = v[i];
      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}

int main() {
 int i, x;
 printf("Please enter the number of terms: ");
 scanf("%d", &x);
 int arr[x];   
 printf("Please enter the terms: ");
 for(i = 0; i < x; i++)
 scanf("%d", &arr[i]);
 START
 permute(arr, 0, sizeof(arr)/sizeof(int));
 STOP   
 printf("Permutation Count: %d\n", perm_count);
 PRINTTIME
 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-29T15:11:49+00:00Added an answer on May 29, 2026 at 3:11 pm

    1. Incorrect access modes in fopen call
    You open your file as a binary file: fopen("char-array.txt", "wb");. Don’t put b to this string containing access modes if you are going to write formatted strings there. And since you probably want to append new data at the end of the file instead of overwritting them, use a instead of w:

    fopen("char-array.txt", "a");
    

    2. Writing to the output buffer, not directly into the file
    When you are using functions like fprintf, you don’t write directly to the file but to the output buffer. You have to use fflush to write data from the output buffer into the file, or you can just close your file by using fclose function which flushes this buffer automatically.

    Just add this line:

    fclose(fpIn);
    

    at the end of print function.

    3. Incorrect formatting of the output
    You should not cast int to char. It will truncate your numbers. And you also have fprintf(fpIn, "\n"); in wrong scope I guess. It could look like this:

    for (i = 0; i < size; i++) {
      printf("%d ", v[i]);
      fprintf(fpIn, "%d ", v[i]);
    }
    perm_count++;
    printf("\n");
    fprintf(fpIn, "\n");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

static int write_stream(const void *buffer, size_t size, void *app_key) { char *stream = (char
Ok, consider the following code: private const int THRESHHOLD = 2; static void Main(string[]
I'm trying to enter edit mode on a specific cell like this: void MainWindow::on_addButton_released()
I was looking for a way to call the edit method directly. - (void)tableView:(UITableView
Is it legal? class SomeClass { public: static void f(); }; using SomeClass::f; Edit:
DWORD type = REG_NONE; int i = 0; size = sizeof(ValueName); size2 = sizeof(ValueData);
A. Base* Base::copy(Base*); Base *Derived::copy(Derived*); B. Base* Base::copy(Base*); Derived *Derived::copy(Base*); C. ostream& Base::print(int,ostream&=cout); ostream&
I understand that a const pointer can be declared a couple ways: const int
Considering this code fragment: struct My { operator const char*()const{ return my; } }
EDIT: minor fixes (virtual Print; return mpInstance) following remarks in the answers. I am

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.