Question
C Programming

222 2 points Write a function for the following specs: • type: int parameter: character array Behavior: • Open the file using
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE

#include <stdio.h>

//function prototype

int read_file(char *);

int main()

{

char *file_name;

//asking user to enter filename

printf("Enter file name: ");

scanf("%s", file_name);

//checking if file has been opened successfully using function

if(read_file(file_name) == 0)

printf("File opened successfully");

else

printf("File could not be opened");

return 0;

}

//the function definititon

int read_file(char *fname)

{

//declaring a file pointer variable

FILE *f;

//opening the file in read mode

f = fopen(fname, "r");

//if file was opened successfully, return 0

if(f)

return 0;

else

return 1;

}

OUTPUT WHEN FILE DOES NOT EXIST

OUTPUT WHEN FILE EXISTS

Add a comment
Know the answer?
Add Answer to:
C Programming 222 2 points Write a function for the following specs: • type: int parameter:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C PROGRAMMING ONLY 22 2 points Write a function for the following specs: • type: int...

    C PROGRAMMING ONLY 22 2 points Write a function for the following specs: • type: int • parameter: character array • Behavior: o Open the file using the parameter for the file name in read mode. o Return O if the file opened successfully, 1 if unable to open the file. В І о A- A Ex x, EE 12 23 2 points Write a function for the following specs: type: void • parameters: FILE", int[], int size Behavior: o...

  • C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[],...

    C Programming 23 points Write a function for the following specs: type: void parameters: FILE", int[], int size • Behavior: Write the text value of each element from the int[] to the file File format: an integer on each line of the file. Le: 4 3 1 5

  • c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments:...

    c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...

  • c++ • return type function name (parameter); • Ex: int Joe(int, int); • Ex: void gg(int,...

    c++ • return type function name (parameter); • Ex: int Joe(int, int); • Ex: void gg(int, float); • Ex: double Two_sol(double, double, double): 2. fC → function call • Ex: cout << Joe(); • Ex: Joe(5, 0); 3. fD → function definition return type function name of STATEMENT(S) return function name: **program84 Void functions to print "Welcome to the world of functions!" programs. To print the sum and product of 2 numbers using functions w/out parameters. program86: to print the...

  • write a function called readAny having the following declaration: void readAny(fstream &iofile, char buffer[], int size, int start, int index); with five parameters, iofile is the fstream variable...

    write a function called readAny having the following declaration: void readAny(fstream &iofile, char buffer[], int size, int start, int index); with five parameters, iofile is the fstream variable for reading the file. Assume that the file has already been opened. buffer is the character array for returning the bytes that will be read in by the function. size specifies how many bytes to read in. start will have one of three constants 1, 2, or 3. 1 means from the...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • Need to check if File is successfully opened? C++ It works when I input the right...

    Need to check if File is successfully opened? C++ It works when I input the right txt file. But, it keeps stating, File successfully open." even I put in a non-existing file. Here are the specifications: Develop a functional flowchart and then write a C++ program to solve the following problem. Create a text file named first.txt and write your first name in the file. You will be reading the name of the file from the keyboard as a string,...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT