Homework Help Question & Answers

C Programming 222 2 points Write a function for the following specs: • type: int parameter:...

C Programming

222 2 points Write a function for the following specs: • type: int parameter: character array Behavior: • Open the file using
222 2 points Write a function for the following specs: • type: int parameter: character array Behavior: • Open the file using the parameter for the file name in read mode. • Return Oif the file opened successfully, 1 if unable to open the file.
0 0
Next > < Previous
ReportAnswer #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

clang-7 -pthread -lm -o main main.c } ./main Enter file name: ayush.txt File could not be opened I

OUTPUT WHEN FILE EXISTS

> clang-7 pthread -lm -o main main.c } ./main Enter file name: chegg.txt File opened successfully>

Know the answer?
Add Answer of:
C Programming 222 2 points Write a function for the following specs: • type: int parameter:...
Your Answer: Your Name: What's your source?
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++ Programming

    1) Array/File FunctionsWrite 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 functionshould 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 arguments: the name of a file, a pointer to an int array, and the size of the array....

  • 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...

  • 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,...

  • Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS:...

    Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS: it is for a yahtzee game program. Write function sumChance to do the following a. Return type int b. Parameter list array of dice c. Declare int variable sum d. Declare int variable die e. Loop through the dice array adding the value of each die to the variable sum f. Return variable sum Write function checkLgStraight to do the following a. Return type...

  • plz using Python3 to do this challenge [ ] # Complete the following and upload it to Canvas # Write a function "...

    plz using Python3 to do this challenge [ ] # Complete the following and upload it to Canvas # Write a function "get-file" to ask the user for a file name. # Return an open file handle to the users file in "read" mode. # Use exception handling to deal with the case that the user's file # does not exist, printing an error saying "File does not exist, try again" # and trying again f -get file() f.close() [...

  • 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...

  • C Question: Write a function p3 which receives a C string as a parameter, and an...

    C Question: Write a function p3 which receives a C string as a parameter, and an array of integers which will serve as indices into the string. The third parameter is the length of the integer array. The function jumbles the characters in the string according to the indices in the array of numbers. For example: char course [] = "CSC 373 Computer Systems I"; int indices[ ] = {0,1,4,5,6,7,0,1,25}; Then the function call p3(course, indices, 9); would cause course...

  • C++ Programming Write a function that takes an integer array, the array size and a number....

    C++ Programming Write a function that takes an integer array, the array size and a number. And it determines how many times that number appears in the array. Just copy the following code and paste it to your answer. And fill the corresponding part. #include <iostream> using namespace atdi int howlany (int array lint , int number //Your code comes here int main() const int N-10: int array) - 11,3,2,1,3,4,6,9,1.3): int count - howlany(array,,3) cout << 3 < appesa" <<...

Free Homework 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.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.