-2
FILE *fre;
int re_rng;

fre = fopen(file_name, "a");
while (!feof)
{
    fscanf(fre,"%d", &re_rng);

    if (fre==NULL){
        printf("Error");
    }
    printf("%d", re_rng);
}

The text file looks like this (vertical list)

12
2
7
5
4
10
15
4
18
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    what did you try, is the code provided gives any error? – Lalit Verma Nov 29 '17 at 07:02
  • You should check the return value of `fscanf`. –  Nov 29 '17 at 07:06
  • 1
    Note that `feof` is a function pointer, and will never be null, so the loop never be entered (unless negation has blown my mind). You probably intended to use `while (!feof(fre))` but [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/). You should probably include some separator between the numbers on output; using `"%d\n"` makes sense, but there are many other options also available – Jonathan Leffler Nov 29 '17 at 07:06
  • 2
    1. Check the return value from `fopen` 2. This is wrong `while (!feof)` on many levels - For a start read the manual page for `feof` and also https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong. 3. Why the test `if (fre==NULL){` inside the loop, 4.... – Ed Heal Nov 29 '17 at 07:06
  • You may want to see [How do I scan numbers to an array from a file?](https://stackoverflow.com/questions/19914298/how-do-i-scan-in-numbers-to-an-array-from-a-file?rq=1) – David C. Rankin Nov 29 '17 at 07:08
  • You really should start with a C tutorial. The `fre == NULL` test should occur immediately after the open, `if(feof)` is plain wrong as explained by JonathanLeffer, you should instead test `fscanf`return value. And `fopen(...,"a")` open the file in write only mode, you should use `"r"` for read mode. – Serge Ballesta Nov 29 '17 at 08:56

2 Answers2

0

To make as few changes to your code, you can do something like this

FILE *fre;
int re_rng;

fre = fopen("vals", "r");
if (fre==NULL){
    printf("Error");
}

while (fscanf(fre, "%d", &re_rng) == 1)
{
    printf("%d\n", re_rng);
}

Things to note: You put wrong specifier at in the fopen, a means append, it is opening the file for writing at the end of the file. You want to use r for reading.

You need to check if the file is ok right after you opened it. If you first try to read and then check, if it failed the program will end with segfault probably.

You can also move the fscanf into the condition for while, where it returns number of conversion made, meaning for you that it returns 1 when it reads a number, 0 when it fails to read a number and -1 when it's at the end of file.

I would suggest you try to read about all of these functions (+feof) at some documentation sites like cppreference.com.

Ordoshsen
  • 650
  • 3
  • 15
0

There are a number of ways to approach this problem. If you have a file with nothing but numbers in it, and you are not taking any other mixed input with scanf, then it can be use to read each integer value in the file until EOF is encountered. Nothing special is needed.

The following code reads from the filename provided as the first argument (or from stdin by default if no arguments is provided)

#include <stdio.h>

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

    int i;  /* read from 1st arg (or stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }
    while (fscanf (fp, "%d", &i) == 1)  /* validate 1 conversion */
        printf (" %2d", i);         /* output your integer */
    putchar ('\n');                 /* tidy up with newline */

    if (fp != stdin) fclose (fp);   /* close file if not stdin */

    return 0;
}

Example Input File

$cat dat/verticallist.txt
12
2
7
5
4
10
15
4
18

Example Use/Output

$ ./bin/rdintsfromfile <dat/verticallist.txt
 12  2  7  5  4 10 15  4 18

Look things over and let me know if you have any further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85