Question

Question 7 (a) Write a C program that performs the following tasks: 1. Create a text file to store a list of numbers. 2. Read

0 0
Add a comment Improve this question Transcribed image text
Answer #1

=======================================

(a) C-PROGRAM TO STORE NUMBER IN FILE

=======================================

#include <stdio.h>

void main() {
/*declare a FILE pointer*/
FILE *filePointer;
/*declare int variable number to read number from user*/
int number;
  
/* open the File "ListOfNumbers.txt" in write mode and store in filePointer*/
filePointer = fopen("ListOfNumbers.txt", "w");
  
if (filePointer == NULL)
{
printf("File ListOfNumbers.txt cannot be created!");
}
else
{
number = 0;/*initialize number to 0*/
while(!(number<0)) /*as long as number is not negative*/
{
printf("Enter number (negative number to stop): ");
scanf("%d", &number);/*read the number*/
/*if number is not negative, write the number in file*/
if(!(number<0))
fprintf(filePointer,"%d\n",number);
}
printf("\nThe file ListOfNumbers.txt created successfully!");
}
  
/* close filePointer*/
fclose(filePointer);
}

=====================================

CONSOLE OUTPUT

=====================================

Enter number (negative number to stop): 5
Enter number (negative number to stop): 10
Enter number (negative number to stop): 15
Enter number (negative number to stop): 20
Enter number (negative number to stop): 25
Enter number (negative number to stop): 30
Enter number (negative number to stop): 35
Enter number (negative number to stop): 40
Enter number (negative number to stop): -100

The file ListOfNumbers.txt created successfully!

====================================

GENERATED FILE ( ListOfNumbers.txt )

====================================

с ListOfNumbers.txt 5 10 15 20 25 30 35 40

==================================================

(b) C-PROGRAM TO READ NUMBERS FROM FILE

==================================================

#include <stdio.h>

int main()
{
FILE * filePointer; /*declare a File pointer filePointer*/
int number; /*declare integer variable number to read integer into*/
  
/*open NumberList.txt file in read mode*/
filePointer = fopen("NumberList.txt", "r");
  
printf("The numbers in the file NumberList.txt are:\n");
/*read first number from file in number variable*/
fscanf (filePointer, "%d", &number);
while (!feof (filePointer)) /*as long as file is not ended*/
{
printf ("%d\n", number);/*print the number variable in console*/
fscanf (filePointer, "%d", &number); /*read the next number from the file*/   
}
// close the file
fclose(filePointer);
}

==================================

INPUT TEXT FILE (NumberList.txt)

==================================

Numberlist.txt LC 51 55 65 173 끼 85 103

===================================

OUTPUT IN THE CONSOLE

====================================

The numbers in the file NumberList.txt are:
51
55
65
73
77
85
103


Add a comment
Know the answer?
Add Answer to:
Question 7 (a) Write a C program that performs the following tasks: 1. Create a text...
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
  • Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

    Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() - Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text. - Use the try/except method to create the file if it does not exist - If the file does not exist, prompt the...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • This program is used to create a phonebook for storing contact information into a text file....

    This program is used to create a phonebook for storing contact information into a text file. The program begins by asking the user to provide a name for the file that will contain their phone book information. It will then ask the user to provide the names and numbers of their contacts. It should keep asking the user for contact information until they type in Done (case-sensitive). After the user types Done, store all contact information into the file name...

  • ***This program is to be created using PyCharm Pro*** 1. Print “This program manipulates text from...

    ***This program is to be created using PyCharm Pro*** 1. Print “This program manipulates text from files and the clipboard and performs other string, file, and directory manipulation tasks.” 2. Output the current working directory of the program. 3. Ask the user to enter the directory where they would like files to be stored, make that directory, and store files in the program in that directory. 4. Write the following three lines to file_one.txt (without bullet points): • Line 1...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

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