Writing to a file

This is a discussion on Writing to a file within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to programming with c and I'm stuck with a small problem. I need to write a c ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    Writing to a file

    Hi, I'm new to programming with c and I'm stuck with a small problem. I need to write a c program that opens a file in note pad.
    The first line is the number 5, for how many rows of binary strings are to follow. Each line contains a string representing a binary number (with a maximum of 32 binary digits)
    The program is supposed to read this file, and convert each binary number to base 10.

    This is the binary code to test:
    5
    0110010101010100100101
    10110
    10000
    01101101111001
    1000000000001

    And the programs supposed to write the results to a file called "results.txt"

    here is what I've go:

    Code:
    #include <stdio.h>
    int main (){
    /* file input guide */
    
    
    FILE *fPtr;
    
    
    int i=33;
    int j=6;
    char name [33]= "d1.txt";
    for (i=0;i<33;i++){
    
    
        sprintf(name, "binary%03d.txt", i);
    
    
    
    
    if((fPtr = fopen("data.dat","w")) == NULL){
    
    
       printf("file data.dat could not be opened\n");
    }else{
       /* file processing follows */
    
    
       fprintf(fPtr,"5\n0110010101010100100101\n10110\n10000\n01101101111001\n1000000000001\n",i,j);
    
    
       fclose(fPtr);
    
    
    }
    
    
    }
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    For starters, you need to format and indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int main() {
        /* file input guide */
    
        FILE *fPtr;
    
        int i = 33;
        int j = 6;
        char name[33] = "d1.txt";
        for (i = 0; i < 33; i++) {
            sprintf(name, "binary%03d.txt", i);
    
            if ((fPtr = fopen("data.dat", "w")) == NULL) {
                printf("file data.dat could not be opened\n");
            } else {
                /* file processing follows */
    
                fprintf(fPtr, "5\n0110010101010100100101\n10110\n10000\n01101101111001\n1000000000001\n", i, j);
    
                fclose(fPtr);
            }
        }
        return 0;
    }
    Next, it looks like you are writing to the file rather than reading from it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing in a file
    By Daniel Santos in forum C Programming
    Replies: 8
    Last Post: 01-20-2012, 06:01 AM
  2. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  3. Replies: 3
    Last Post: 11-21-2006, 06:26 PM
  4. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 01:17 PM
  5. Not writing to a file.
    By sideshow_bob in forum C Programming
    Replies: 2
    Last Post: 02-19-2003, 10:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21