Help! Coding a Program with Limited Knowledge of C/C++

Click For Summary
SUMMARY

The discussion focuses on creating a simple C++ program to read two inputs, "toll" and "norm", and write them to a file named "data.txt". The user seeks guidance on file handling, specifically how to overwrite an existing file and format the output correctly. The solution involves using the fstream library to manage file operations, with the ofstream class for output. The provided code snippet demonstrates how to open the file, write the formatted data, and suggests that explicit file closure is not necessary for simple programs.

PREREQUISITES
  • Basic understanding of C++ syntax and structure
  • Familiarity with input/output operations in C++
  • Knowledge of file handling using the fstream library
  • Experience with data types and formatting in C++
NEXT STEPS
  • Explore file handling in C++ using fstream and ofstream
  • Learn about formatting output with cout and file streams
  • Research error handling in file operations to manage exceptions
  • Study the differences between C and C++ for file I/O operations
USEFUL FOR

Students learning C++ programming, beginners needing to understand file operations, and anyone looking to implement basic data logging in their applications.

omgitsroy326
Messages
29
Reaction score
0
I have a program due in couple hours. Professor expects us to know C++ when we never really went over programming; he expects us to just know it .. anyhow


I have two inputs used with scanf

it's toll and norm

these two values need to be put into a data file created by the program

I need a key that allows me to
create a "data.txt" file and if the file exist to overwrite it by erased etc.. doesn't matter and put in the things as

printf(" Toll = %e \m Norm = %e", toll, norm);

and just exit the program

i know it's coded in C... but that's all i know
anyhow if i can code in C or C++ doesnt' matter also could you tell me what library i can use.
 
Technology news on Phys.org


Originally posted by omgitsroy326
I have a program due in couple hours. Professor expects us to know C++ when we never really went over programming; he expects us to just know it .. anyhow


I have two inputs used with scanf

it's toll and norm

these two values need to be put into a data file created by the program

I need a key that allows me to
create a "data.txt" file and if the file exist to overwrite it by erased etc.. doesn't matter and put in the things as

printf(" Toll = %e \m Norm = %e", toll, norm);

and just exit the program

i know it's coded in C... but that's all i know
anyhow if i can code in C or C++ doesnt' matter also could you tell me what library i can use.

Hi omgitsroy

I don't have my C++ book on me, so the syntax may be a bit off.

Code:
#include <fstream> // File operations

.
.
.

ofstream output("data.txt" , ios::out) ; //Opens a file object 'output' to file "data.txt" 
                                         //for output, overwrites if existing

.
.
.

output<<"Toll = "<< toll <<"\m  Norm = "<< norm << endl; //Outputs to file just like 'cout' command

I don't remember how to explicitly close the file, but I don't think it's really necessary unless your program is complex. Formatting can be done with standard i/o commands.

Hope that's all correct...
 
thanks a lot
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
5K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
47
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K