C++ :: Going To A Specific Line Through Ifstream
Jan 12, 2015I want to access specific lines in a "*.txt" file. I've heard of seekp() and what not, but don't know how to call them as such.
View 6 RepliesI want to access specific lines in a "*.txt" file. I've heard of seekp() and what not, but don't know how to call them as such.
View 6 RepliesI have a file blah.txt like this:
3 1 4
1 2 3
and here is my code to read in that file:
Code:
int x = 0, y = 0, z = 0;
ifstream data("blah.txt");
while(x != 1)
{
data >> x >> y >> z;
}
For some reason, it just keeps readin in the values: 3 1 4
I am reading in some large files to process. The files need to be parsed into multi-line sub units for processing (by a different physical process). My current setup is crude and parses the entire file into memory before beginning to process the sub units. This is fine as long as the file fits, so I get into trouble > 2GB or so when my machine flat runs out of memory. The simple thing to do is to only read in some of the file, process what was read in, and then read in more. I more or less know how to stop reading at some point in the file, but I'm not sure how to resume from that point later when I need more data. Is there a way to count lines and then getline() starting from a specific point in the file? From reading the getline() doc, it doesn't look like there is.
This is my current function,
Code:
void ParseFile(char *path, char type) {
// open input file
ifstream input_file(path);
if( !input_file.good() ) { cerr << "Failed to open " << path << endl; exit(ERRCODE_ERROR); }
int pos = 1;
[Code] ....
I need to remove the code to open the file from this function and open it elsewhere. I would like to call this every time I run out of data, if( tasks.empty() ), and get more data from the input file, but I don't know how to resume reading the input file from where I left off.
If this is not possible, I guess I would have to place a function call in while( input_file.good() ) to call out and process the data I have. When the current tasks list is finished, the list could be cleared and control returned into the while loop to continue reading input and start re-populating the list.
I am currently working on a small project, which is an ATM simulator but, lately, I've been stuck in a part where I have to overwrite a specific line in a .dat file to update the account's checking and savings balance, once the transaction has been done. I have to use StreamReader and StreamWriter to complete the task and I have also being told to try Collections... which I'm not sure as to why.
Here's my code so far:
private void btnOKd_Click(object sender, EventArgs e) {
int transaction = 0;
atmTIMER.Elapsed += new ElapsedEventHandler(timerRESTART);
FileStream fileTRANSACTIONS = new FileStream("C:\Users\Voyage\Desktop\ATMSim\ATMsim\Transactions.dat", FileMode.Open);
[Code] .....
Some codes to delete a text in a specific line in a text file.
Example:
This is line 1
This is line 2
this is line 3
Removing line 2,
This is line 1
This is line 3
//////////////////
I am suppose to make a program that will output a line for a specific age group,but everytime I execute the exe file it gives me the desired line and also the last line included everytime.Where did I go wrong with it including the last line everytime?
#include <iostream>
using namespace std;
int main() {
double age;
cout<<"Enter your age:";
cin>>age;
if (0<age && age <6)
[Code] ......
How can I create in my C++ program so a user can paste a text and then choose a character on specific line and place?. The above thing isn't that important. I just want to place a character on a specific line and place.
I mean something like this:
Enter a character:
You choosed " / "
On which line do want the character?
You choosed "Line 1 and 2"
Where do you want the the to appear on the line? (left or right)
You choose left.
Results:
Line 1. / hello
Line 2. / hello
How do I make a specific character show up a specific amount of times?
Like I am generating a random number then I need to make "|" show up that many times on the screen.
How do I convert ifstream to binary and display the binary at the end. I have a file that when it contains numbers it can do it but when reading strings it has trouble. It acts as if there is no binary in the file.
View 17 Replies View RelatedCode:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);
[Code] ......
I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.
I am currently working on a C++ program for school. I am actually not finding too much difficulty in constructing the functions, enum-types, arrays and structs, however, I am finding great difficulty in using on ifstream variable to open multiple files.
I have posted the entire code that I have so far (even though I have pinpointed the issue to not properly opening the second file in ifstream).
I spent a couple of hours getting rid of certain functions/procedures, loops and variables and I get the same output (if what I removed doesnt crash it). I also get the same output whether I "open" the second file or not (meaning I removed all of the code for it and got the same output).
Here is the code (it's not finished because I am stuck on this file issue). It's a bit messy since I am now in debug mode versus program mode:
I want to create a function with a return type. However, I want to use it to read from a file (ifstream) and produce multiple different types of return types. The different file types returned would be always in the same order. For example
Text File:
Name
1
12
30
Area
I want to ifstream line 1 (Name) to an array of characters.
line 2, 3, and 4 to integers.
and line 5 as a string.
The basic problem is that if I make a function with one return type, it would only return one type of data to my int main(). I suppose I could create multiple functions that would run this for different variable types and destroy the invalid types. But this seems inefficient. It is being used to load in data from a previously saved file.
So far..
//function to load a player's data
string loaded(string fileName) {
ifstream loadfile;
loadfile.open(fileName);
[Code] .....
I was thinking I could possibly do something with a counter to count the lines and assign a value based on their order. The problem is with the 1 return value of a function. Maybe there is another operation I could use?
I wrote a program which was supposed to decrypt a file encrypted with the XECryption algorithm. Now, I know the decryption algorithm, but I have a problem with my ifstream object. It doesn't read anything at all, and the ofstream object just outputs a single random byte in a never ending loop. I've tried using cin, which works correctly, but it's not what I want.
Here's the code:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in("file.in", ios::in);
if (!in.is_open())
[Code]....
I'm doing this on a Windows 8(.1) pc with Code::Blocks 13.12. In the file (file.in) I have replaced the points with spaces. What is wrong with the code?
If I have a data file.dat like this, for example:
...
other information
...
physics:
4,5,6,7
...
line:
(4,2), (2,4)
line:
(1,2), (2,3)
...
...
I want to design a class and corresponding code so that every time when it reads "line:" for the file.dat , it will push_back a new line into the line_t vector, and each time when it encounter physics it will put the values to physics, How can I implement this?
every data in file.dat is useful, they need to be read into different class type. How can I implement this?
class line_t {
public:
vector<point_t> P(2, point_t());
[Code].....
I have a text file that ifstream opens and fills some fields with the data. I've been able to get at the file, by using the precise path, but that path would be incorrect on other computers I'll be working on the project at. So, is there default location I should drop the folder in, and route to with the path? If not which Directory listing in Visual Studio 2013 should I use so the project can find the folder?
This is how I've formatted the string for ifstream.
".DataSampleActorData.txt"
Is there a way to make this program work without entering the full path to a file?
Code:
ifstream ex("podatki.txt", ios_base::in); // if I type full path than program works
if(ex.is_open()) cout << "The file is open and con. with the object." << '
';
else cout << "you messed up" << '
';
int counter = 0;
[Code] ....
I am having an issue with passing an ifstream object to functions. Here is the code:
Code:
#include <fstream>
using namespace std;
void otherfunction (ifstream *ifs) {
...does stuff, like ifs->open(), then reads from the file...
}
int main () {
ifstream ifs();
otherfunction(&ifs);
}
Here is the error message:
Code: error: cannot convert ‘std::ifstream (*)()’ to ‘std::ifstream*’ for argument ‘1’ to ‘void otherfunction(std::ifstream*)’
Why can't I do that? What does "ifstream (*)()" even mean? And I don't want to change the structure of the program. I have reasons for declaring the ifstream object in the main function (because there are actually two functions that need access to the ifstream object -- neither of which is working).
Also, if I change the main function to be this instead:
Code:
int main () {
ifstream ifs();
ifstream *ifsptr = &ifs; //EDIT 2: forgot the ampersand
otherfunction(ifsptr);
}
I get the same error as above. However, if I change the main function to this:
Code:
int main () {
ifstream *ifsptr = new ifstream();
otherfunction(ifsptr);
}
I get all kinds of crazy errors about "undefined symbols for architecture _____". Here is the actual error message from my program (parseArgs is the real name of otherfunction)
Code:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cchumGBV.o
"std::ios_base::Init::~Init()", referenced from:
[Code] .....
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
I've been working on a student data base that reads in the students name, birth date, social security, and department name (or major). I have all these items in the header files respectively; nameType, dateType, personType, and studentType.
I am now to create another header file called HWONEHEADER that contains the functions showMenu, loadStudent, insertStudent, searchByName, and SaveStudents. This is how far ive gotten
#ifndef HW
#define HW
#include <iostream>
#include <fstream>
#include <iomanip>
#include "studentType.h"
[Code] .....
Here is the cpp file i have.
int main() {
studentType department;
cin>>department;
cout<<department;
[Code] .....
I need with the HWONEHEADER using the ifstream and ofstream operators to load all the students information into a file called student.dat I'am extremely confused because i can't find anything in my textbook about using ifstream and ofstream operators.
How to open a file that the user input
ifstream InFile;
ofstream OutFile;
InFile.open("???.txt")
I am trying to run the code below but I receive the following error message :
Debug Assertion Failed!
Program: C:TestDebugTest.exe
File: c:program filesmicrosoft visual studio 10.0vcincludevector
Line:932
Expression:vector subscript out of range
Code:
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <strstream>
#include <cstring>
#include <cmath>
#include <map>
#include "Swap.h"
#define SIZE_X 100
[Code] .....
I'm doing a project that takes in a input file with Quarterback statistics. It has their name, team, completions, sacks, touchdowns, etc. It has 16 different variables in all (17 if you count first/last name as two). So I'm trying to read them and I can't figure out how to jump to the new line after it's read the file and put the information in the variables I've created.
#include <iostream>
#include <fstream>
using namespace std;
ifstream din;
void openFile() {
[Code] ....
That's the program I've written. The loop keeps displaying the first line of the file over and over. How can I get it to go to the second line, then the third, then fourth, etc? I need it to display all the lines of the file until it reaches the end of the file.
I have been trying to figure this out for a text based game. Lets say your money is 500. Then you save your game, but when you start the game the starting is 50, so when you load it, you still have 500 money, I tried lots of test, and im having trouble?
View 10 Replies View RelatedI'm trying to pass an reference of the object ifstream to my constructor as such:
// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}
I am defining the constructor as follows, in myClass.cpp:
myClass::myClass(ifstream &file){
// do stuff
}
I get the error of "No overloaded function of myClass::myClass matches the specified type."
Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"
Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>
What am I doing wrong? How can I pass an ifstream reference to my constructor?
I need to write a ANSI program to print out each command line argument on a separate line using a for-loop. also it need to print the name of the executable .so far I have
Code:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
printf("")
[code]....
l need to write a program which writes out its command line arguments in reverse order one per line. The output from the program should look like this:
% a.out Two roads diverged in a yellow wood
wood
yellow
a
in
diverged
roads
Two