C++ :: Monitoring Progress Of Subroutine That Is Using File Stream As Input?
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.
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)
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.
I am trying to extract unsigned values from an input stream. I expect the extraction to fail when an invalid character is extracted. It fails correctly when I try to extract an unsigned int from "abc", but when I try to extract an unsigned in from "-1", the extraction succeeds, and the max unsigned int value is extracted (as if -1 were cast to unsigned int). I would expect the '-' to cause the extraction of an unsigned value to fail.
The code I am using is below.
#include <iostream> #include <sstream> #include <string> #include <limits> int main() { unsigned int value = 8; std::string negString = "-1";
[Code]...
Is this standard behavior for an istream extractor?
I am trying this in both Linux (gcc 4.4.3) and in windows with Code::Blocks (whaterver came with CB 13.12, apparently gcc 4.7.1)
Can we use more than one 'if' subroutine in one program. I am trying to implement this but if I run the two subroutines at the same time the lower subroutine directly shows "invalid grade". But if I run one at a time then they work perfectly fine.
Why is this happening? Does the compiler think that its the same "if" routine. If so how do I segregate it???
Code: printf("Enter a score:"); scanf("%d",&gr); if(gr>=80 && gr<=100) printf("A Grade "); if (gr>=60 && gr<80) printf("B Grade
I need to create a program that reads some numbers, and calculate them on a separated subroutine, and the return of this subroutine must be the sum of all the numbers. I'm getting an error but I can't figure out why =/
#include <iostream> using namespace std; int calc(int val, int qtd){ int sum=0; for (int cont=0; cont<qtd; cont++) sum=sum+val; return sum;
[Code]...
The error that I'm getting is on the line 22.
The error message: "invalid conversion from 'int*' to 'int' [fpermissive]
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);
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;
#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.."
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?
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++.
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) {
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?
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.
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?
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());
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);
the problem with my code is that trace toggle can only be turn on, not off and trace does not demarcate the (substring) in progress. how do i fix this?
Am developing windows forms application where in my form am fetching data into list from some object which takes around 2 minutes to complete processing so meantime i want to show progress bar before starting process till it ends with percentage. I have return a code to do multitasking but it is throwing error as "Cross-thread operation not valid: Control 'listOrg' accessed from a thread other than the thread it was created on".
private void btnOrg_Click(object sender, EventArgs e) { try { // To report progress from the background worker we need to set this property backgroundWorker1.WorkerReportsProgress = true;