C++ :: Sequential Reading Blocks Of Lines From A File

Aug 14, 2014

I have a file that I need to read in blocks. I need to read in n lines at a time, do some processing, and then read in the next block of n lines until the file is done. I know the size of the block, but not the number of lines in the file unless I check that first. Normally I would read in like,

Code:
// declarations
string new_input_line, input_file;
// create an input stream and open the input_file
ifstream input_file_istream;
input_file_istream.open( input_file.c_str() );

[Code] .....

// process through data_block

With this approach, I'm not sure how I would keep looping to read the next block until I hit the end of the file without knowing how many lines are in the input file. I could process the file to find that out, or get that number from bash and pass it it as an argument, but it seems like that shouldn't be necessary.

I could also read in and store the entire file as a vector of string and then process through it afterwords, but that would not be a very efficient use of memory.

View 1 Replies


ADVERTISEMENT

Visual C++ :: Looping Lines And Blocks Of Characters

Mar 17, 2013

I have a file that is like the following, with patterns of pipes in it:

Code:
||| || | |
| ||| | | |
|| || ||

I have to consider each row of pipe characters to be in blocks of 3 characters each (e.g. - positions 1-3, then 4-6, etc, etc...) but I have to capture all of the pipes, in sequence, like so:

positions 1-3 for lines 1-3, then positions 4-6 for lines 1-3, etc, etc...

How to get this done besides writing severely redundant control structures like loops, one after the other?

View 5 Replies View Related

C++ :: Reading From Specified Lines In File?

Jul 13, 2013

i want to read text between two indices in the file using file i/o operations in c++. HOw can i do it? for example, here is the text file:

1:
..
..
2:
..
..
3:
..
..
4:

in this text file i need to print only the lines between 2: and 3:. How could this be done?

View 1 Replies View Related

C++ :: Reading Lines From Text File

Jun 3, 2014

I am trying to read lines from a .txt file like this:

// "file.txt"
This is the first line.
This is the second line.

To do this, I tried using the following code:

int main() {
std::ifstream data;
data.open("file.txt");

[Code] ....

The output I want is:

This is the first line.
This is the second line.

However, all I am getting is:

This is the first line.

I can't quite see why this isn't working. I am decently familiar with file streams in C++, so I thought I would know what I was doing, but apparently not.

View 1 Replies View Related

C++ :: Reading Lines From A File Into Array?

Sep 1, 2014

The first part of the exercise is to read the lines from a file placing each line into an array. I thought my code looked correct however nothing but garbage prints out. Here's my code.

#include <iostream>
#include <fstream>
#include <string>

[Code]...

View 2 Replies View Related

C :: Reading From A File And Skipping Lines To Find String In File

Nov 26, 2013

Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.

The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.

The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.

The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)

The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.

The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.

The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.

The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:

If a patron buys a ticket at the door, a command will be on a line by itself:

BUY TICKET k

where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.

Code:

#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000

[Code].....

View 4 Replies View Related

C++ :: Reading Number In Lines In Text File

Feb 27, 2015

lets say this is our textfile

-----------------------------------
45(here is space)string and other shit is here
454(here is space)string and other shit is here
4121(here is space)string and other shit is here
77(here is space)string and other shit is here
45545(here is space)string and other shit is here
1122(here is space)string and other shit is here
-----------------------------------

how do i get exactly that number in every line start? and compare but i jus tneed to get them to variable so ?

View 3 Replies View Related

C++ :: Reading Random Lines From A Text File

Apr 29, 2013

how to read random lines from a text file using "C", without repeating same lines.

View 10 Replies View Related

C :: Reading Lines From A File And Storing It In Array From Typdef Struct

May 4, 2013

I'm trying to read in lines for a file and storing it in an array. This is what I have so far. Am I allocating the memory correctly?

Code:

typedef struct {
/* declartion of variables */
} App
typedef struct {

[Code].....

How do I access the array content? I'm suppose to strtok the contents from the lines stored in the array and use it again.

View 2 Replies View Related

C/C++ :: Reading Lines Of Numbers / Characters While Checking For EOF?

Aug 30, 2014

Simple program to convert files to XML files. However I am trying to narrow down why my code is not working. I am reading a line that looks like "2001 Joe Dirt Los Angeles", then the next few lines are followed by text and the the fifth line looks like "---End of Description---".

So here is the file

2001 Joe Dirt Los Angeles
Home is where you make it
Best movie ever(but not really)
But seriously
---End of Description---

Here is example code to test the process

string line;
while(dataFile >> line) {
getline(dataFile, line);
cout << line << endl;
}

My returning output is

[space]Joe Dirt Los Angeles
[space]Home is where you make it
[space]Best movie ever(but not really)
[space]But seriously

My test code is skipping the date on the first line but replacing it with a space. Then it is skipping my fifth line which reads "---End of Description---". Now if I take the code out of the while loop and hard code in five lines of output (x5 getline(dataFile,line); cout << line << endl;) then my code works as expected. I get all of the information. Date and the fifth line.

It looks like when I am testing for the EOF it is also taking in the numeric value at the beginning of the first line. However, that does not explain why it is dropping that fifth line.

View 3 Replies View Related

C/C++ :: How To Append Txt File In Code Blocks

May 14, 2014

I've been using code::blocks to work on text files through fstream. But i cannot make it append using my code.

Here's my code:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream dataFile;
dataFile.open("demofile.txt", ios::out);

[Code] ....

View 2 Replies View Related

C :: Added Header File In Code Blocks

Aug 27, 2013

In my c project in code blocks , I have added another source file (KillerMoves.c) and a header file under the same name (KillerMoves.h). I added the "include guard" to prevent double inclusion. But from some reason, the program suddenly crashes with no warning or specific error. Without the new files,the program runs without any problems. This is the source .c file:

Code:

#include "KillerMoves.h"
void initKillersTable(){
for(int i = 0; i < 15; i++){
killersTable[i] = -1;

[code]....

View 11 Replies View Related

Visual C++ :: Reading Character Symbols On Lines Of Text Position By Position?

Mar 4, 2013

What I have to do is write a small program in C++ to parse the symbols that are used on 5 different lines of text in each position until position 30 is reached on each line. The goal of the parsing program is to interpret the symbols (characters), if there are any per each position, on the 5 lines of text in order to output the actual data that the group of symbols represents.

My question for is this: Is there anything special from a C++ environment that should go in to something like this outside of using standard stuff like the math associated with the search algorithm that has to happen here? The symbols are located in a file, so I know I have to include "iostream" and a few other headers. But outside of header inclusions and the code necessary to iterate and streamline the search and interpretation process, am I missing anything special that I couldn't otherwise find through simple google searches?

View 6 Replies View Related

C++ ::  threads To Take Sequential Turns

Jun 30, 2014

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int turn = 1;
int counter = 0;
int t, n;

[Code] ....

I found this program in a forum and it creates 1 to t threads with each thread pointing to the next and the last thread pointing to the first thread and it allows each thread to sequentially take a turn until all threads have taken n turns. That is when the program ends.

My doubt is if we see in void *tFunc(void *arg) function, first thread locks the mutex and then wait there forever till it's turn comes.Lets say if I spwan 1000 threads and they have take 10 turns each sequentially and what if a 550th thread locks the mutex as soon as the program starts and waiting for its turn which will never happen because still (turn=1) which means it's first thread's turn and first 549 threads have to complete their 1st turn before this 550 threda's turn comes.Doesnt this result in deadlock?

View 2 Replies View Related

C++ :: Update Records Using Sequential Files?

Nov 3, 2013

How do i go about updating a specific record in sequential File using a primary key in c++. I used Inventory number as my primary key..

Code: int locate[2];
string fname, lname, add, name, address;
int num, foundit;
Customer customer;

[Code].....

View 6 Replies View Related

C/C++ :: Saving Sequential Files To Computer?

Feb 2, 2015

[size="5"][size="4"]

I am programming in C and am having some trouble finding the .txt file I saved after having run my program. It is likely a stupid error on my behalf. However, it is causing me quite a bit of grief at the moment. I attached my source code below  
abgcchp27ex1.txt (2.01K)

I coded my structure within my header file(bookInfo.h) correct along with everything else. My dilemma is a matter of my file location and whether it actually saved.

/* This program takes the book info program from chapter 27 and writes the info to a file named bookinfo.txt. */

// First include the file with the structure definition

#include "bookInfo.h"
#include
#include
FILE * fptr;

[Code]....

View 3 Replies View Related

C/C++ :: Sequential Matrix Multiplication - Error In Execution Time

Apr 30, 2014

I wrote program for Sequential matrix multiplication .But after execution for any input value( ex. 100,150,400) it shows the execution time is 0.000 msec.

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
void print_results(char *prompt, int N, float *a);
int main(int argc, char *argv[])

[Code] ......

View 2 Replies View Related

C :: Erase From TXT File Lines That Contain One Name?

Feb 4, 2013

Well, I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). BUT, I don't want the values from the lines with the saying "interpolated_vector" to be printed in any of the three files.

What I have is the code below, that has the code that creates the three new files with the columns of values that I want. It is working fine!

Code:

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
int i, count;
double *a, *b, *c;
double *x, *y, *z;
char tag[5][255];

[Code]...

I've tried and tried, but couldn't make it work properly. I could only erase one line with "interpolated_vector" using fgets, but all the other lines below this were printed into the three new files (file1, file2, file3).

View 5 Replies View Related

C++ :: Counting Lines From A File

Apr 23, 2013

How I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work.

void SpellCheck::checkValidWords(const string& inFileName) {
string eachword;
ifstream istream;
istream.open( inFileName.c_str() );
if ( !istream.is_open() ) {

[Code] .....

View 3 Replies View Related

C++ :: Read Only Certain Lines From CSV File

Oct 21, 2014

I am trying to read lines from a CSV file and put the values into vectors. The CSV contains a large amount of data and I would like the program to only read certain lines from the CSV into the vectors. An example of the data is below. In my code the user inputs a secid. I would then like the code to only read lines from the csv where the secid matches what the user inputs. The secid's are also not in order so I can't just use a while loop.

My current code is below the data.

secideffect_datecusip ticker
10131014MAY19972313510 AMZN
10131008MAY19982313510 AMZN
10131028NOV20002313510 AMZN
10131023JUL20012313510 AMZN
10196601JAN199663858510 NB
10196601OCT199806605F10 BAC

ifstream infile3("filepath.csv");
if (!infile3) {
cerr << "Couldn't open file!"<<endl;
return 1;

[Code] .....

View 1 Replies View Related

C++ :: Read Certain Lines From A File?

Aug 22, 2014

I want to read certain lines from a file. Let say if the line contains word "makes", the line will be loaded on the screen.how to modify this code.

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;

[Code] ....

View 1 Replies View Related

C/C++ :: Deleting Certain Lines In Txt File?

Dec 27, 2013

I was assigned to modify Game Lobby code. All the data can be saved in txt file and can be displayed when running the code..How can i remove or delete the certain lines of a txt file when running the cmd?.. I cant program it to be able to delete certain lines but i manage to program to delete the entire data, which is not what i want..

Here's the code so far...

#include <iostream>
#include <fstream>
#include<vector>
#include<cstdio>
#include<iomanip>
#include <string>
using namespace std;
class Player {
public:
Player(const string& name = ""): m_Name(name), m_pNext(0) {}

[Code] ....

View 3 Replies View Related

C :: How To Count Lines In Dat File Using Fscanf

Sep 15, 2013

I need a way to count lines in a .dat file using fscanf. I must use a regex to check that the characters in the file are alphanumeric. In essence I need to count the ' ' character. I know fscanf ignores this character. I need to exit if it detects anything other than alphanumeric with the digit that is "problem" along with the line number. My .dat file is:

Code:
howard jim dave
joe
(
Maggie My fileCheck.c is: Code: #include "main.h"
int fileCheck(FILE *fp)
{
int ret_val;
int line_count = 0;
char file[BUFF];

[Code]...

The output I am getting is:

Code:
file opened Digit: ( is not an alphanumeric character on line: 5 Program will exit! File closed As you can see, the character "(" is not on the 5th line but the 3rd. It is the 5th "string."

I also wanted to show the regex I am using.

Code:
#define to_find "^[a-zA-Z0-9]+$"

How this can be accomplished?

View 5 Replies View Related

C :: Counting Number Of Lines In A File

Feb 2, 2015

it looks like a popular method for determining the total lines in a file is to read the entire file character by character in search of ' '. I have a file with 5 lines, but for some reason this code isn't finding any instances of ' '. Is this possible? Is there a better way to get the number of lines in a file?

The file looks like this:

Code:
NAME: John
FRIEND 1: Steve
FRIEND 2: Andrea
FRIEND 3: Ken
OCCUPATION: Programmer

Code:
FILE *f = fopen(currentFile, "r");
if (f == NULL) perror ("Error opening file.
");
int ch, lines = 0;
while(!feof(f)){

[Code] .....

View 2 Replies View Related

C++ ::  Loading File With Multiple Lines

May 17, 2014

I'm trying to load a file that look like this:

IP:123.123.123.123
Port:12345

I can't figure out how to load multiple lines. I tried using , but I can't get it to work. Is there a better way of doing it than I am right now? This is my code.

char message[100];
int messageCount = 0;
for(unsigned int i = 0; i < file.size(); ++i) {
message[i] = file[i];

[Code] ....

View 1 Replies View Related

C++ :: Attempting To Get Data From Lines In A File?

Mar 30, 2014

For a project we are given a file with a couple of thousand lines of values, each line having 9 different numbers.

I want to create a for/while loop that, for each loop, stores the 8th and 9th integer of a line as a variable so that I can do some calculations with them. The loop would then move onto the next line, store the 8th and 9th numbers of that line as the same variable, so that I can do the same calculation to it, ending when I've run out of lines.

My problem is less to do with reading the file, I'm just confused how I'd tell it to take only the 8th and 9th value from each line.

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved