C++ :: File Stream And String Extraction?
Apr 7, 2014
I am currently trying to read in a file that takes in the input in the following form.
Code:
HANK>25 BOB>31 AL>54
BILL>41 ABE>63 JEFF>50
I have tried the following solution:
Code:
#include<ifstream>
#include<iostream>
using namespace std;
struct node
{
string name;
int age;
);
int main ()
{
[code]....
The problem is a.name, it's extracting the entire string before the space character causing p.age to contain "BOB" and so on. I've tried using p.get(p.name, sizeof(p.name), '-') with '-' as the delimiter and p.getline() using a character array instead of a string. How would it be possible to only copy the string into a.name before the '>' character?
View 1 Replies
ADVERTISEMENT
Oct 1, 2013
I wrote a class that can display fractions ex. 1/4 and I cannot figure out how to get >> to process 1/4 and separate them into variables numerator and denominator.
my program just constantly creates RationalNumber Objects when it reaches cin >> A .
my overloaded stream extraction function:
istream& operator >> (istream& in, const RationalNumber& rn)
{
char L;
[Code].....
View 1 Replies
View Related
Apr 29, 2013
I wrote a program to write text contents to file stream through fputs, the file stream address was changed in the middle of writing text content to the stream (11% text content have been put into the file stream), that cause the file stream pointer can be evaluated problem and raise exception on stream validation code in fputs library function, my question is what things could go wrong to make file stream pointer changed its address to something else or a NULL pointer if the file stream have not been flushed and closed.
View 5 Replies
View Related
Jan 21, 2014
I have two puzzling issues I am dealing with.
Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.
Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?
View 7 Replies
View Related
May 9, 2014
Debug Error
ProjectsFinal ProjectGrItemDebugGrItem.exe
R6010 - abort() has been called
I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.
Quick Code
The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.
// Read regular price
getline(nameFile, input, '$');
vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
// Read sale price
getline(nameFile, input, '#');
vectorList[count].salePrice = stof(input.c_str());
Pastebin Links : [URL] ....
View 2 Replies
View Related
Oct 17, 2013
This is my code:
#include <iostream>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
int main() {
string groups[3] = {};
[Code] ....
It outputs jibberish. I can do what I need to do but I would need to declare more variables and write more cout's, isn't there a way to add these elements to a stringstream or streambuffer? My goal is to write this program and make it as comprehensive as possible but also with very few lines.
View 1 Replies
View Related
Oct 2, 2013
I have problem with this code:
#include <windows.h>
#include <stdio.h>
#include <gdiplus.h>
#include "ScreenCap.h"
#include <wchar.h>
#include "base64.h"
#include <sstream>
using namespace Gdiplus;
int GetEncoderClsid(WCHAR *format, CLSID *pClsid) {
[Code] ....
This function return me a long series of Y ended with other chars but not the base64 string.
I think the problem is std::string encodedstring = base64_encode(buffer, dwBufferSize); but I can't figure out the problem.
The base64 function is: [URL] ....
View 2 Replies
View Related
Sep 2, 2013
I'm reading in a data set using an ifstream, then fetching line by line to a stringstream:
std::ifstream sfile(filename);
std::string test;
std::getline(sfile, test);
std::stringstream sline(test);
(I'm not sure why I used an intermediate string; it's pretty much legacy-code at this point, which I just reuse every time. Still works, so why change it!)
The problem is I'm using two types of data sets now, and the difference is one (optional). Most data files just have an arbitrarily large number if the second must be ignored, but others have nothing.
In the normal case, I'd simply use sline >> d >> L; to extract the parameter values. However, I'm not sure how this line will behave if the second parameter is omitted. Will it read nonsense? How do I check whether or not the parameter was set or not?
View 7 Replies
View Related
May 14, 2013
I am working on a project which is a fairly simply BAC calculator. I am attempting to grab data from a text file, which contains state abbreviation, maximum BAC before aggravated, and minimum license suspension. The text file looks like
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <ctype.h>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <sstream>
using namespace std;
float MALE_RATE = 0.68, FEMALE_RATE = 0.55, LEGAL_LIMIT = 0.08;
//percent of body weight which holds alcohol
[Code] ....
The code I am struggling with is the getline and stringstream which is near the bottom.
The output I am getting is
The maximum BAC before aggrevated DUI in AL is .15
The minimum penalty in AL is 90 day suspension of license
I get that for every state I type. I know it is reading the first line, but something in my code does not allow it to read the other lines
View 1 Replies
View Related
Oct 7, 2012
I am working on a project were I have to read line form ( PLC ) programmable logic controller generated text file with lines like this
Circuit Value Current 2.33 4.32 5.55
there could be up to 3000 lines per txt file
I am using string stream to parse the line, for the sake of good programming I which to check weather first three values are string and last three values are actually floats raise or throw an exception if they are not ....
View 4 Replies
View Related
May 24, 2014
I've written this code to extract everything that is between "/*" and "*/". Unfortunately it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main () {
int x = 0;
[Code] ....
View 5 Replies
View Related
Apr 6, 2013
I want to extract expressions like <23.34 463.2 23.2 + *> in program runtime from the user. (I am trying to evaluate postfix expression)....
View 3 Replies
View Related
Dec 4, 2013
Why does my loop loops 3 times when i have only 2 line of data in my file? From the cout << s.size , when i run it will show 1,2,3.
Information in txt file
Code:
local john 10/2/1990
international tom 2/5/2000 output Code: local john 10/2/1990
international tom 2/5/2000
international tom 2/5/2000 Code: ifstream in(filename);
[Code] ....
View 4 Replies
View Related
Mar 4, 2013
I'm having some trouble with copying one I/O stream into another. I've put the first one into an array but I cannot get my second prompt to copy the .txt file the first prompt sees and outputs to the console. When I try and grab the info from the .txt file my first prompt sees I only see blank space in my .txt file.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
using std::ofstream;
[Code] .....
View 1 Replies
View Related
Oct 13, 2013
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
I have used the above code to write the class instanc to a file "Text.txt"...But it seems that "f.write((char*)&c, sizeof(CLS));" is not working properly with string. The data can easily be written using stream object!
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
When I tried to read the data on the file...., it gave an error "Thread stopped...violation.."
View 7 Replies
View Related
Sep 20, 2013
Let us assume I have a program that takes the name of the output file from the command line. Now let us assume that a decided not to give any output file location but wanted my program to to print my strings/ints ... directly to stdout. how would one do this using file streams? Using file pointers and c that is pretty straight forward but how would i achieve this in c++.
View 12 Replies
View Related
May 9, 2014
#include <cstdlib>
#include <stdio.h>
#include <string>
[Code]....
I'm trying to load a file input stream to load all the character content into a variable (say from a .java file for example), but for some reason whenever I type in the name of the file I want to stream I get the report: RUN FAILED (exit value 1, total time 2s)
View 7 Replies
View Related
Apr 26, 2012
I have function which will replace (or create) an file with the contents of another stream. The stream could be anything. The replacement is done safely.
Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
int do_replace(const char *file, int stream, int cnt) {
[Code] .....
View 14 Replies
View Related
Nov 10, 2014
I am trying to do some exercises but am struggling at the moment. The first task was to create a class (Planet), then allow the user to edit their entry or view it.
The second one is to create class instances from a text file. The file contains a new planet on each line in the form: 'id x y z' where x/y/z are its coordinates. As the file can have more then one lines, it has to dynamically create an undefined amount of class instances.
To do this I used 'new' and it works ok - it prints each one out to the screen as you go so you can see it working. However... I'm trying to get into good habits here and am encapsulating the class which is where I am getting stuck. I can read from the class but cannot put the values from the file into the class.. ..using the member functions I have created anyway.
My code so far is:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Planet {
private:
int id=0;
float x_coord=0.0, y_coord=0.0, z_coord=0.0;
public:
int GetID(){return id;}
[code]....
If I change the SetID etc to just p->id, p->x_coord etc it works fine. But I'd rather find a way to do it that keeps the encapsulation. Using p->z_coord etc requires that you change the class variables from private to public.
The question I have been given is this:
Define and implement a function, generate planet, that takes a stream argument that has already been connected to a file (i.e. the argument is istream& fin). The function must create a new instance of planet using new and read its details from the next line in the file.Each line of the file is in the format id x y z.The function must return the newly created planet.
Also, how would you go about 'viewing' one specific class instance once they've been created? So say the file had 5 lines, line three was '4 6 2 6'. How would I go about viewing that planet afterwards? I don't think thats required but... I'm just wondering Although I'm also wondering, are we actually creating a new class instance for each line here? Or just destroying the previous one?
View 14 Replies
View Related
Jun 23, 2013
What is the traditional way to monitor a blocking subroutine that is using a file stream as its input? That is, what is the traditional way to make a progress meter in the console?
Say I have a function that takes in a filestream and works with it. Suppose this file stream has a position and length property like in C# FileStream:
Code:
void sub (filestream file)
{
//read a bit of the stream, which advances stream cursor position
//do something
//repeat until all of file stream has been used up
}
Presumably the function is chomping along the file stream as it is doing some calculations serially on the file stream.Because the function is blocking, there is no way to access the file streams position and length in this thread. So naturally it seems like the best thing to do to monitor the progress of that function is to make another thread and pass it the file stream object as a parameter, and in this separate thread, monitor the distance between filestream's position and length to determine how much of the file stream has been used up, and every second or so output the amount of file stream used onto the console.
View 3 Replies
View Related
Feb 12, 2014
So I'm trying to count the number of lines in a text file that is inputted by the user. Also the code doesn't sum up the last number of the text file (exmp it calculates and print only 14 from 15 numbers). I'm a beginner in c++ programing.
View 3 Replies
View Related
Mar 27, 2013
Write a program which reads a stream of numbers from a file, and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() will open both files. Another function named process() must then be called to read all the numbers from the input file and write the positive numbers to the output file. Note that you must pass the open stream variables for each file as arguments to the process() function, and that you need to (always) double check that the files opened successfully before using them.
This is what I have so far but its not working out!
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int process(ifstream &inf, ofstream &outf);
[Code] ....
View 1 Replies
View Related
Aug 23, 2012
I am trying to stream data to a file, and then return to the file to add further data. When I add data the second time, I then want to update the value of the second byte in the whole file. I can't seem to do this!
Here is my sample code:
Code:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int x;
fstream out1("file.dat", ios::out | ios::binary | ios::trunc);
[code]....
The output I get is "1, 2, 3, 4, 5, 6", but I want to be getting "1, 7, 3, 4, 5, 6", because in "out2", I seekp to the second integar entry, and change it to "7".
I have also tried using ios::ate in the constructor for "out2", but this gives me the out put "4, 7, 6, 6, 6, 6", which is suggesting that when I create my fstream object "in", any seekg commands are relative to the beginning of the "out2" stream, rather than the "out1" stream.
View 3 Replies
View Related
Feb 24, 2013
I'm trying to display an image from a stream data. But there is no image when getting image::from stream.
It's my source code:
Code:
IStream* pstream = NULL;
if(SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, &pstream))) {
ULONG lreal = 0;
pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
[Code]...
There is no image from data.
View 5 Replies
View Related
Feb 6, 2013
Do stream iterators, such as std::istreambuf_iterator<char>, read a chunk of bytes internally, or do they read the stream one byte at a time?
Because I am thinking to write a BufferedFile class which uses an std::vector<char>.
View 3 Replies
View Related
Mar 10, 2014
I'd like to be able to output sound over skype as if it were coming from my headset, how would I go about this?
View 1 Replies
View Related