C++ :: Read And Rewrite Lines In File
Jan 2, 2015
My task is to write a function for borrowing books form library called borrow(name,surname, student_id, book_id), which will connect information of borrowed book's ID and student's information in the file.
File looks like this:
John Jackson 45 0
Michael Gregory 34 56
Ann Cawitch 23 0
Chris Lamb 34 50
...
First two words are name and surname of students. First number represents student's ID and second one represents ID of book. If ID of book is 0, it means that student hasn't borrowed book yet, so he can borrow some book. If ID of book is not 0, it means that student can't borrow a book until he returns the old one. So, when student borrows book, ID of book in file should change from zero to some number
This is my code:
void borrow(string name, string surname, int student_id,int book_id ) {
string a;
string b;
int c=0;
int d;
fstream f2;
f2.open("students.txt", ios::out |ios::in);
[Code] ....
Whenever I call function (for example borrow(John, Jackson,45,15)), I get answer "Student isn't sign up library."
Am I on a right path? How to make this code to work?
View 4 Replies
ADVERTISEMENT
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
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
Apr 10, 2014
when I use Net Reactor encrypt my project ,it will create xxxx_secure folder, But i hope Direct rewrite original file,how to do ?
View 1 Replies
View Related
Oct 21, 2013
I have to txt files, and want them to read into an array line by line and after then a split the lines with delimeters, at the first file I use ";", at the second file I want to split the lines to single words by spaces. My aim is to get the first word from the first file and compare with all the words from the other file, to check is there any matches.
My problem is, that at the first file works everything fine, and I want to do the with the second file, but after reading the lines into lines_input[], I check the an empty = null line, I get segmentation fault.
You can see the code below:
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
//Change the
[Code] .....
View 1 Replies
View Related
Jan 31, 2014
I am writing a code to read a file and break it into lines and characters but I am getting an error do not know the reason for it though.
here is my code
#include<fstream>
#include<string>
#include<iostream>
[Code].....
The first line is executing properly but when it goes to the second line I get an error "the program has stopped working"
View 1 Replies
View Related
Jan 12, 2014
private void open(object sender, EventArgs e) {
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == DialogResult.OK) {
string[] lines = File.ReadAllLines(openDialog.FileName);
int k = 0;
while (k < lines.Length)
[Code] ....
So in this code, the user basically opens a text file from the dialog. Then the file gets read on all the lines and gets stored in an array. Now the reason I did this is because there are more fields of text boxes I have to fill in after I fill in the table (6x3). I'm getting an out of bounds error however.
19
19
19
0
95
0
0
0
0
5
0
0
5
1
0
0
51
1
110
Warrior
This is a sample text file that is being used. As you can see all the lines from start up until (excluding "110") will be used in the table. Now I want to read the last lines in this text file and put it in another text box. How do I do that?
View 2 Replies
View Related
Jan 25, 2013
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). The pattern of the source text file is like this:
known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089
Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
View 2 Replies
View Related
Aug 22, 2013
I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.
I'm getting this error :
"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"
#include<iostream>
#include<string>
#include<fstream>
void displayInfo();
using namespace std;
class Employee{
[Code] .....
View 1 Replies
View Related
Feb 21, 2014
I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs. It needs to read in another input file that has a list of words (1 per line) to ignore. i.e. skip them when running through the story. This is what I have so far, I've changed multiple things trying to get it running....
#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include"split.h"
[Code] .....
View 1 Replies
View Related
May 31, 2013
I am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?
is it something like string line;
getline(stream,line);
??
View 7 Replies
View Related
Oct 6, 2014
how could this program tell the ID's and Passwords of more than one line in the text document?
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
[Code].....
View 2 Replies
View Related
Apr 23, 2014
This code works, but not fully This code reads com ports, when a string is available, I check to see if it contains some keywords defined in array If it does, i print out the string itself.
The problem i am having is that it seems to stop processing the data at some point but I do know that there are more strings to be read. So, it does keep reading more lines of data
using System;
using System.Text;
using System.Collections.Generic;
[Code]......
View 9 Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
Aug 26, 2014
I am working on a problem for the CS50 online course through the Harvard web site. I am trying to re-size a bmp image. They gave us a small program that copies a bmp image and we are to modify it to re-size the image. Here is the page [URL]..... Any way I am trying to move the file position indicator backwards so I can read the same line from the input file. So I can resize the file vertically I have the image resized horizontally but it keeps over writing the same lines vertically.
/**
* bmp.h
*
* Computer Science 50
* Problem Set 5
[Code]...
View 4 Replies
View Related
Jun 3, 2013
I was trying to make a function that would return how many lines are in a certain file. I was looking at [URL] which talks about how to count how many characters are in a text file. I just want the number of lines with in a file.
View 2 Replies
View Related