0

How exactly do I take an input from a file in C?

As in, for instance: Say i assigned a file pointer to a particular file and want to peform certain operations. What exactly is the syntax for assigning the file pointer Assume the file, is located at C:\Acads\bin\File.txt. In my code when I try this

FILE *fp1;
fp1=("C:\Acads\bin\File.txt","r+");

It ends up giving me an error.

UPDATE: Okay so here is my main doubt.

How exactly do i tell the compiler that my file is located at so and so path. I've tried doing everything you guys have told me but to no avail.

2
  • Just for fun (and debugging) print the name of the file: printf("The file is named %s\n", "C:\Acads\bin\File.txt"); :) Commented Dec 22, 2011 at 13:31
  • Just googling for your question title gives tons of valid results. Commented Dec 22, 2011 at 13:41

5 Answers 5

2

I think you ran into a typo: It should say:

fp1=fopen(filename,"r+");

Also you have to escape each backslash char \ with another backslash:

fp1=fopen("C:\\Acads\\bin\\File.txt","r+");

This is because the \ begins an escape sequence. E.g: \n would mean a newline. \\ is expanded to a simple backslash.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay yea i forogt '\' was a special charcater. But inspite of doing what you told me , it still isn't working... My next steep is c=getc(fp1); printf("%c",c); when i try running this code, it gives me an error
I made a typo myself, it should be fopen and not open. Also please give the exact compiler/linker error to let us indentify the problem.
FILE *fp1; fp1=fopen("C:\\Academics\\gcc\bin\\pe.txt","r+"); printf("opened file\n"); c=getc(fp1); printf("%c",c); The compiler does not give me any error, However when i run the program it does not print the desired output.
Please give all the information. What actually happens? Does the program print anything? Does it crash? Did you try debugging?
I don't really know how to explain myself here. I can compile the programme, and it does not show any warnings or errors. But when i click run, a windows pop up screen comes and says my program has stopped responding. This occurs with me for instance when i run into an infinite loop or when i try to divide by 0
|
1

This should give you an overview of file handling in C. You can use fopen to open a file & use perror to see if something went wrong. Something on these lines:

FILE *fp;
fp= fopen("C:\\Acads\\bin\\File.txt","r+"); /* You have to escape \ in C as it is a special character*/
if ( fp == NULL)
{
  perror("fopen");
  /*Handle error*/
}
/* File operations */
fclose(fp);

Hope this helps!

1 Comment

Did you get any error message from perror? Is fp fine? If you are using getc please remember that it returns int & not char.
0

Here's a simple example of using fopen

FILE *fp;
fp = fopen(path, "r+");

Take a look at man fopen.

Comments

0

You should open the file before you try to read. Here is a complete example of how to open a file and read consequent lines.

http://www.phanderson.com/files/file_read.html

1 Comment

Yes but over here, they haven't mentioned where the file is stored. As in how does the program know where the file is located? Thats what im not understadning
0

A file pointer is a pointer to a struct containing certain information about the file, like where it's located, how big it is and other things depending on your operating system and libc implementation.

Now, the point is that you don't need to worry about what that struct really contains. You just know that you can have a pointer to one and that the library functions know what to do with it. This gives the wonderful feature of portability, meaning that your code can work unaltered even on systems which have a completely different way of handling files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.