473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Program inputs .txt file, reads from it, calculates values, and prints to output .txt

2 New Member
Hello everyone,

I'm just beginning down the road of programming, so take it easy on me. lol

I'm writing a program that will take information off of an input file(.txt), if the file name/path is correct it will calculate the needed values, and then print the results to an output file(.txt). It will check for errors after the user has input the file path and after the user enters the output file(.txt) that the program will print to. I am having trouble with the file error for the output file. Everything else is working fine, but for some reason(and I'm sure it's something small) it will print to the file but when I check to make sure that my if(!out) will catch errors with the file path, that portion doesn't work. I've been racking my brain for a couple days now. Writing, testing, rewriting, testing... I've written the program several different ways that have all led to the same problem, so I could deffinately use some help on this!!


Here is the portion of code that I'm having trouble with...

......
Expand|Select|Wrap|Line Numbers
  1. cout<<os.str(); 
  2. outputofstream out; 
  3. string OutputFile; 
  4.  
  5. cout<<"Enter output file name (Use FULL file path):  \n\n"; getline(cin, OutputFile); 
  6. out.open(OutputFile.c_str()); 
  7. cout<<endl;
  8.  
  9. if(!out){ 
  10.     cout<<"Invalid output file.\n\n";
  11.     cout<<"Ending program now.\n\n";
  12.     exit(1);     
  13.     }
  14.     else{ 
  15.         out<<os.str(); 
  16.     } 
......


I hope that is easy to read, but again, I know the problem is somewhere in here. I had it working at one point but when I corrected another mistake I forgot what I had changed.(Rookie mistake lol) But according to the examples we did in class this looks exactly the same, but it is not working in the same what
Jul 11 '13 #1
2 1437
mastajoda13
2 New Member
Here is what my program looks like
Attached Files
File Type: docx include.docx (16.6 KB, 343 views)
Jul 11 '13 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
I presume you are concerned the code does not compile.

I did the following:

1) replaced the string OutputFile with the string os
2) defined os before using it in a cout
3) replaced outputfstream with ofstream (there is no outputfstream)in the C++ library

BTW: Since os is a string object you can just cout the string. No need to use the c_str() method. However, you still need the c_str() method in the open because down deep in there the C fopen is called and it needs a char* C string.

I did not test your code.
Jul 11 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: tomektomeknyc | last post by:
I did an interactive program to input and store employee paycheck information including total net pay into txt file. I can read it from txt file but how can i lets say modify it to calculate the...
0
by: indacrypt | last post by:
Hi, I'll post detailed code if required, but if someone has alreayd had this kind of a problem before and can help me out, that'll bemost appreciated. Theres a particular function in my program...
1
by: TJ | last post by:
I am very new to C# (this is my first real project), therefore please be patient if my question is considered being to newbie. I am modifying a program which takes a text file, does some...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
5
by: unknown418 | last post by:
I am confused what is the efficient way to read in a file with multiple values per line. Each value is separated by a space character. The values could be characters or real values. I am using...
1
by: star01daisy | last post by:
This is what i have, but when i run the program the only thing that comes out is the inro line "This program opens...". What do i have to do for it to run smoothly.The program is supposed to sort the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.