Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please fix my problems

+5 votes
asked Apr 8, 2020 by pwla mea2546 (170 points)
reshown Apr 15, 2020 by pwla mea2546
Write a C  program that reads from the keyboard a natural number n with up to 9 digits and creates the text file data.out containing the number n and all its non-zero prefixes, in a single line, separated by a space, in order decreasing in value. Example: for n = 10305 the data file.out will contain the numbers: 10305 1030 103 10 1.

Program in C++:

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    unsigned long n;
    cin>>n;

   //I declare the file f and open it for data writing
   ofstream fis("file.out");
   while(n!=0)   
   {
   //write the number in the file, then delete the last digit
   fis<<n<<'\n';
   n=n/10;   
   }
   fis.close();
   return 0;
}

That's what i made in C:

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

int main()
{
    int n;
    scanf("%d",&n);
   FILE *f;
   f=fopen("date.out","w");
   while(n!=0)   
   {
   printf(n);
   f=fopen("date.out", "r");
   n=n/10;   
   }
   fclose(f);
   return 0;
}

3 Answers

0 votes
answered May 21, 2021 by Shashi Panta (170 points)
Inside while loop, I guess you tried using fprintf() function to add to the file but it is not there. I hope this solves your issue. Incase you need solution try this.

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

int main()
{
   int n;
   printf("Enter the n digits number:\t");
   scanf("%d",&n);
   
   // create file pointer
   FILE *f;
   f=fopen("data file.out","w");
   
   
   
   while(n!=0)   
   {
       // write into the file
        fprintf(f, "%d ", n);
      // get the quotient stored in n or remove the last digit every time loop runs
        n=n/10;   
   }
   
   fclose(f);
   return 0;
}
0 votes
answered Jul 2, 2021 by Knight Owl (140 points)
//PROGRAM IN C:

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

int main()
{
    int n;
    printf("INPUT: ");
    scanf("%d",&n);
    
    FILE *f;
    f=fopen("date.out.txt","wb");
    while(n!=0)
    {
        fwrite(&n,sizeof(n),1,f);
        n=n/10;

    }
    fclose(f);
    
    FILE *r;
    r=fopen("date.out.txt","rb");
    printf("\nOUTPUT: ");
    while(fread(&n,sizeof(n),1,f)>0)
    {
        printf("%d ", n);
    }
    fclose(r);
    
    return 0;
}
commented Jul 7, 2021 by Peter Minarik (55,760 points)
The original problem description (implicitly) talks about the output being a text file. You're using a binary file instead.

Also, there's no need for the string.h header.

Otherwise a fine solution and using binary data saves disk space (just this wasn't what the original description asked for).
0 votes
answered Jul 7, 2021 by Peter Minarik (55,760 points)
edited Jul 7, 2021 by Peter Minarik

C++ Code

The C++ version of your code runs, but there is room for improvements:

  • You call your output stream fis (file input stream). This is confusing. Call it file, or output stream or something that reflects on the true nature of the variable.
  • The instructions said separate your number by space, but you used new line instead.
  • using namespace could make your code shorter, but it could also pollute namespaces, so it's a good practice (especially in larger code bases) that you instead fully qualify the name of your functions.

C Code

You had some compilation issues here and general logic problems.

  • You should check the documentation on how to use the printf() function.
  • You never wrote to a file, instead tried to open the file multiple times over and over in the loop.
  • There's no need to include the string.h header.

General Comments

  • It is generally a good idea to tell the user what s/he's supposed to do (e.g. print the instruction before reading the input).
  • Also a good idea, if one checks if the file was successfully opened before you start reading/writing it.
  • In OnlineGDB writing to an output could be tricky. Some file names cannot be used for some reason (system reserved files perhaps). One of them would be "data.out". Instead, as you did, a different file name could be used when the code is run on OnlineGDB.
  • For the above reason, I added a const char * fileName to both solutions so the file name can easily be replaced if needed.

The Solutions

Here's the enhance C++ solution:

#include <fstream>
#include <iostream>

int main()
{
    const char * fileName = "file.out";
    std::cout << "Please, enter a number: ";
    unsigned long n;
    std::cin >> n;
    
    std::ofstream file(fileName);   // I declare the file and open it for data writing
    if (!file.is_open())
    {
        std::cout << "Could not open file: " <<  fileName << std::endl;
        return -1;
    }
    
    while (n != 0)
    {
        file << n << ' ';   // Write the number in the file,
        n /= 10;            // then delete the last digit
    }
   
    file.close();
    return 0;
}

Here's the enhanced C solution:

#include <stdio.h>

int main()
{
    const char * fileName = "file2.out";
    printf("Please, enter a number: ");
    int n;
    scanf("%d", &n);
    
    FILE * f = fopen(fileName, "w");
    if (!f)
    {
        printf("Could not open file: %s\n", fileName);
        return -1;
    }
    
    while (n != 0)
    {
        fprintf(f, "%d ", n);
        n /= 10;
    }
    fclose(f);
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...