0
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
    
    //opening collection.txt using ptr 
    FILE *ptr;
    
    char data[1000];
    ptr = fopen("collection.txt", "r");
    
    printf("Hello world \n");
    
    fscanf(ptr, "%s", data);
    printf("%s \n", data);
    
    fclose(ptr);
    
    return 0;       
}

collection.txt:

hi my name is 

When I run this program I'm getting :

Hello world 
P7k

P7k is a memory location I assume.

I've looked at multiple websites and articles and I'm unable to figure out what I can do to print the text in collection.txt

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
math101
  • 31
  • 1
  • 3
  • 3
    Add a check that `fopen` succeeded: `if (ptr == NULL) { printf("Could not open file\n"); return 1; }` Also *always* check what all `scanf` functions (like `fscanf`) [*returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value). – Some programmer dude Nov 20 '21 at 13:03
  • @Someprogrammerdude used the if condition but had no luck with that :( checked about the return value of fscanf but it didn't help much – math101 Nov 20 '21 at 13:12
  • 2
    Do `fopen` succeed, and `ptr` is not a null pointer after the `fopen` call? And `fscanf` returns `1`? – Some programmer dude Nov 20 '21 at 13:13
  • @Someprogrammerdude yup I did that exactly, but not luck. And yes fscanf returns 1 – math101 Nov 20 '21 at 13:18
  • If `fopen` succeeds ans if `fscanf` returns 1, that means the there is a file called collection.txt in the currrent directory of the program, and `fscanf` has read the string "P7k` from that file. Check all that that.The current directory might not be what you think is. – Jabberwocky Nov 20 '21 at 14:02
  • 1
    Also replace `fscanf(ptr, "%s", data);` with `fgets(data, sizeof(data), ptr);` and see what happens. – Jabberwocky Nov 20 '21 at 14:03
  • If all works, then the file you open isn't a plain text file. If you open it in a plain text editor, or use a command to type it as plain text, or (if you're on e.g. Linux use the `file` command to classify it), is it really what you expect it to be? You don't have multiple `collection.txt` on your drive? How did you create this file? – Some programmer dude Nov 20 '21 at 20:24

2 Answers2

1

Problems include

Not testing for fopen() success

FILE *ptr;
// add
if (ptr == NULL) {
  fprintf(stderr, "Fail to open\n");
  return -1;
}

Not testing for fscanf() success

// fscanf(ptr, "%s", data);
if (fscanf(ptr, "%s", data) != 1) {
  fprintf(stderr, "Fail to read\n");
  return -1;
}

Not limiting input

char data[1000];
// fscanf(ptr, "%s", data);
fscanf(ptr, "%999s", data);

Not reporting all data

"%s" does not save white space, so white-space from the file is not printed.

Use fgets() to read a line of input. To read all, use a loop.

while (fgets(data, sizeof data, ptr)) {
  printf("%s", ptr);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

%s only scans until it finds a white character. My suggestion would be to scan the file character by character (%c) until you reach the end of the file.

Something like:

while(!feof(ptr)) { 
  fscanf(ptr, %c, data + counter); counter++;
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Jakub Bednarski
  • 261
  • 3
  • 9