0

Full Edit:

I am getting frustrated, I don't know what am I doing wrong in here I still have so many stuff to do in the code but I can't even open a file to continue my work.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
char letter;
FILE *fp;
fp=fopen("‪‪‪C:\\Users\\LENOVO\\Desktop\\data.txt","r");
if(fp==NULL)
{
    printf("error");
    getch();
    exit(1);
}

while(fscanf(fp,"%d",&letter)!=EOF)
putchar(letter);
getch();
fclose(fp);
}‪

Picture of the path: https://i.stack.imgur.com/LulWJ.jpg

Still prints error

Bolar
  • 11
  • 2
  • 1
    `while(EOF!=fscanf(input, "%c", &letter)) putchar(letter);` – BLUEPIXY May 20 '17 at 18:25
  • Thanks for the help – Bolar May 20 '17 at 18:35
  • 3
    We are not a tutoring service. Do you have a specific question not answered in your C book? – too honest for this site May 20 '17 at 18:35
  • I was just asking a question (I know what EOF does but i didn't know how to use it till now) I didn't ask for a full program code – Bolar May 20 '17 at 18:44
  • 1
    @Bolar your new code is wrong, please read the comments more carefully. `while(!EOF)` is always false. The important point is to **always check the return value from `scanf` function family**. I suggest reading the man page for a function that is causing you problems. – Weather Vane May 20 '17 at 20:55
  • "I know what EOF does" hmm, `EOF` does not ***do*** anything, it is a defined value returned from some functions, usually `-1` to signify "End-Of-File". – Weather Vane May 20 '17 at 21:03

3 Answers3

0

Ok, firstly let's take a look at your file path. There are two ways to acces a file from your local storage:

relative addresses if the file has the same root folder as your application

absolute addresses if the file is in a determined place on your machine's storage

I see that you are trying to use an absolute address to read from your file. Your path is correct but you have to take care about string formatting in C because the \ character could be interpreted as something else.

I would suggest to use this instead ( double back-slash )

input=fopen("‪C:\\Users\\LENOVO\\Desktop\\data.txt","r");

This will prevent string formatting interpretations.

Secondly, EOF is just a predefined macro constant and i think it is equal to -1 so your while(! (-1) ) code is not a good ideea for reading until the end of the file.

In order to read from a file until you reach the its end i would consider this property of fscanf() : fscanf() returns EOF when it reaches the end of the file.

while(fscanf(input,"%ch",&letter) != EOF) {
putchar(letter);
}

This way of reading from a file should do the job.

0

To read everything from a text file and store its contents into a buffer:

First, you should count how many characters there are in the text file:

size_t get_file_len(FILE *fp)
{
    size_t num = 0;

    while (fgetc(fp) != EOF)
        num++;

    return (fseek(fp, 0, SEEK_SET) == 0 ? num : 0);
}

Then allocate memory for a buffer large enough and read all the characters:

char *load_text(const char *path)
{
    char *buf = NULL;
    FILE *fp = NULL;
    size_t num = 0;
    size_t i = 0;
    int c = 0;

   /* open the file in text mode */
   fp = fopen(path, "r");
   if (!fp)
       return NULL;

   /* if the file was empty or if an error occurred, return error */
   if ((num = get_file_len(fp)) == 0) {
       fclose(fp);

       return NULL;
   }

   buf = malloc(num + 1);
   if (!buf) {
       fclose(fp);

       return NULL;
   }

   while ((c = fgetc(fp)) != EOF)
       buf[i++] = (char)c;

   /* ensure that the string is null-terminated */
   buf[i] = '\0';

   fclose(fp);

   return buf;
}

Also, in C, all escape sequences begin with a '\' (backslash), so if you wanted to write a backslash in a string or a char you should write it as a '\\' (double backslash):

input=fopen("‪C:\\Users\\LENOVO\\Desktop\\data.txt","r");
oriyon
  • 87
  • 4
-2

pretty simple here :

    while(!feof(input)){
    fscanf(input,"%c",&letter);
    putchar(letter);
    }

and remember to close file using fclose(input);

Biswajit Roy
  • 508
  • 2
  • 7
  • 19
  • 1
    [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – pmg May 20 '17 at 19:26
  • 1
    @pmg I've written `while(!feof(input))` works ! You can verify with **Programming in C - Gottfried**. – Biswajit Roy May 20 '17 at 19:36