Can any one help me me? need a c program to open a file using c

  • Thread starter Thread starter anbhu28
  • Start date Start date
  • Tags Tags
    File Program
Click For Summary

Discussion Overview

The discussion centers around the request for assistance in writing a C program to open a file. Participants explore various methods and functions related to file input/output in C programming.

Discussion Character

  • Homework-related, Technical explanation

Main Points Raised

  • One participant requests a C program for opening a file.
  • Another participant suggests using "fgets" or "fscanf" for reading from a file, depending on the file contents.
  • A different participant mentions that many books on C language provide sample code for file I/O and suggests using Google for additional resources.
  • One participant explains that the simplest way to open a file is using "fopen(Filename, mode)" and provides an example of how to use it to read from a file.
  • The same participant describes how to read bytes from a file using "fread" or formatted strings with "fscanf".

Areas of Agreement / Disagreement

Participants provide various suggestions and resources, but there is no consensus on a single solution or approach to the problem. Multiple methods and resources are discussed without resolving which is the best.

Contextual Notes

Some responses assume familiarity with C programming concepts, and there may be missing details regarding specific file types or error handling in file operations.

anbhu28
Messages
1
Reaction score
0
can anyone help me me? need a c program to open a file using c

pls can anyone send me a c programing code for opening a files
 
Technology news on Phys.org
anbhu28 said:
pls can anyone send me a c programing code for opening a files
Any books talk about c language will give you a sample code to do file IO. Also, you can get too many results by Google
http://www.cprogramming.com/tutorial/cfileio.html
http://www.cs.bu.edu/teaching/c/file-io/intro/
...
 


The simplest way to open a file is "fopen(Filename, mode)" where Filename is the file name as a string and mode is string such as "r" for "read only", "w" for "write", "a" for "append" (add to end of file), and a number of others.
That returns a pointer to the open file that you would then use in reading or writing to the file.

For example,
FILE *f= fopen("myFile","r");

makes f a pointer that can be used to read from "myFile".

You could then use "fread(buffer, 10,10, f)" to read up to 10 bytes from the file to the variable "buffer" (which you would declare as a pointer to the correct type of variable) or you could use fscanf(f, ...) to read a formatted string just as you would with "scanf".
 

Similar threads

  • · Replies 33 ·
2
Replies
33
Views
3K
Replies
81
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
7K
Replies
19
Views
4K