-1

I am currently trying to create a file with a text and then read the file and print it on my screen.My current code is this:

int main(){
    char s[10][100];

    FILE *fpointer;
    fpointer=fopen("file.pp","w");
    int i=0
    while(i<10){ 
        printf("enter a sentence:\n");  
        gets(s); 
        fprintf(fpointer,"%s\n", s); 
        x++;
    }

    fpointer=fopen("file.pp","r");
    int i=0;
    for(i=0;i<10;i++){

        fgets(s[i],100,fpointer);

        printf("the %d sentence is: %s \n",i+1,s[i]);    
    }
    if(fpointer==NULL){
        fprintf(stderr,"mistake");
        EXIT_FAILURE;
    }

    fclose(fpointer);
    return 0;
}

The creation of the file works fine, same with the creation of the text. My problem is the reading of the contents: the first line is displayed correctly followed by the rest of the lines that are just a bunch of symbols. I would imagine that the problem is the way I use fgets but I can't seem to be able to resolve it.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    `gets(s);` -> `gets(s[i]);` (and presumably a transcribe typo: `x++` -> `i++`) – kaylum Dec 31 '19 at 02:45
  • Note that you don't actually need `s` to be a 2D array since you are only reading/writing one line at a time. – kaylum Dec 31 '19 at 02:48
  • Thank you for your answer.I changed it from gets(s) to gets(s[i]) as you suggested and it worked. How would that work without a 2d array though?? The cycle just stops at the first iteration when the char array is 1d. – mikep199 Dec 31 '19 at 03:14
  • 1
    Change `s` to be `char s[100];` and then just use `s` in all the `gets`, `fprintf`, `fgets` and `printf` calls. You don't actually need the previous line's contents so each iteration can just over-write the same `s` buffer. – kaylum Dec 31 '19 at 03:16
  • You also don't need to redeclare `i` later in your program (after you open the file), in fact, you already set it to be 0 in the for loop immediately afterwards anyway, so you can just totally remove that line; a better solution might be to make a local `i` in each loop (i.e. have `for(int i=0; i < 10; i++)`) and refactor your while loop to a for loop. – kopecs Dec 31 '19 at 03:19
  • 1
    Another problem is the location of `if(fpointer==NULL)`. You need to do that check **before** using `fpointer` – kaylum Dec 31 '19 at 03:22
  • 1
    Copy/paste only code that you have tested. Your source does not compile. – Martin James Dec 31 '19 at 04:53
  • 1
    If your compiler lets you use `gets`, you need to throw it away and get a better compiler. If your book tells you to use `gets`, you need to throw it away and get a better book. If your professor tells you to use `gets`, he should be fired on the spot. – n. m. could be an AI Dec 31 '19 at 11:16
  • [About gets](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – n. m. could be an AI Dec 31 '19 at 11:21

2 Answers2

1

The following should work. You have a typo x++ and I changed the input method to fgets. Also for convenience I added the macros NUM_OF_SEN and LENGHT.

#include <stdio.h>
#include <stdlib.h>
#define NUM_OF_SEN 2
#define LENGHT 100

int main(){
    char s[LENGHT];
    FILE *fpointer;

    fpointer=fopen("file.pp", "w+");

    if(fpointer==NULL){
        fprintf(stderr,"mistake");
        EXIT_FAILURE;
    }

    for(int i=0; i < NUM_OF_SEN; i++){ 
        printf("Enter a sentence:\n");  
        fgets(s, LENGHT, stdin); 
        fprintf(fpointer,"%s", s); 
    }

    rewind(fpointer);
    for(int i=0;i<NUM_OF_SEN;i++){   
        fgets(s, LENGHT, fpointer);
        printf("The %d sentence is: %s \n",i+1,s);    
    } 

    fclose(fpointer);
    return 0;
}
davidm
  • 1,570
  • 11
  • 24
  • It's better now, but you don't really need an array at all, you only use one line at a time. Also it's essential to check every call to a library function that may fail. – n. m. could be an AI Dec 31 '19 at 13:24
0

In short, the following code does what you want. Don't forget each line is explained by a comment.

#define LINES 3
#define CHARS 100

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

    // A buffer variable for Writing data to the file
    char writeBuff[CHARS];
    // A buffer variable for reading data from the file
    char readBuff[CHARS];
    // File handler
    FILE *fpointer = NULL;
    // i, counter are just counters
    int i = 0, counter = 0;

    // Open the file to write/append
    fpointer = fopen("file.txt", "a");
    // a Loop to write several sentence
    for (i = 0; i < LINES; i++) {
        printf("Enter a sentence: ");
        // Put the stdin content into the "writeBuff" variable
        fgets(writeBuff, CHARS, stdin);
        // Put the "writeBuff" variable into the file
        fprintf(fpointer, "%s", writeBuff);
    }
    // Close the file that will be opened later for reading 
    // (To make the recently added sentences possible for reading)
    fclose(fpointer);

    // Open the file again for reading
    fpointer = fopen("file.txt", "r");
    // A loop to read each line in the file
    while(fgets(readBuff, CHARS, fpointer) != NULL){
        // Print the file content line by line 
        printf("The %d sentence is: %s", counter+1, readBuff);
        // Just a counter
        counter++;
    }
    // Finally, close the file forever
    fclose(fpointer);

    return 0;
}
Lion King
  • 32,851
  • 25
  • 81
  • 143