Question

In: Computer Science

Write a C program that opens a file called "numbers.txt" in writing mode. The program should...

Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered.

Your program should check to make sure that the file was opened successfully, and terminate if it was not.

Solutions

Expert Solution

c code

#include <stdio.h>

main() {
FILE *fp;

fp = fopen("numbers.txt", "w");
if(fp==NULL)
{
exit(1);
}
float inputnum=-1;
char numtostring[50];
while(inputnum!=0)
{
scanf("%f",&inputnum);
gcvt(inputnum, 6, numtostring);
fprintf(fp, numtostring);
fprintf(fp,"\n");
}

fclose(fp);
}

Sample console output

55                                                                 

6.7                                                                

056.2                                                              

0  

Sample numbers.txt output

55
6.7
56.2
0


Related Solutions

Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function. Can you add few comments with explanations what is going on?
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This...
1. Write a public Java class called WriteFile which opens an empty file called letters.dat. This program will read a String array called letters and write each word onto a new line in the file. The method should include an appropriate throws clause and should be defined within a class called TFEditor. The string should contain the following words: {“how”, “now”, “brown”, “cow”}
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT