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
ADVERTISEMENT
Mar 11, 2013
I am supposed to write a program that reads lines from files and see if the first character is an odd number, if so it will out put the remaining characters to output file, else itll seek to the beginning of the next line, this is what i came up with but its not working as it should, im not sure if its the version im using or the program I wrote, the commented part below was another algorithm I was trying to use.
#include<fstream>
#include<iostream>
using namespace std;
int main() {
char c;
int i =0;
[Code] ....
My text file was as follows:
input.txt
3Ali
4Sami
6Rashid
3Aya
output was unexpected , although some was relatively write but not as I wanted...
View 3 Replies
View Related
Oct 15, 2013
I am trying to print a specific line from a textfile
e.g
I have a text file called info.txt and inside it contains the id,item name, price, quantity
Code:
1,shirt,100,10
2,pants,50,9
3,pen,20,8
I know how to find a specific word at the line in the file but how do I find and print out the specific line e.g(print out just the entire contents of line 2)?
Code:
string temDescription;
ofstream fout;
int curLine = 0;
[Code].....
View 1 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
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
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
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
Apr 29, 2013
how to read random lines from a text file using "C", without repeating same lines.
View 10 Replies
View Related
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
View Related
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
Apr 18, 2014
I am trying to put pieces of a csv file into an array, but i only want to put certain pieces of it. This is the information that i have
Player,Current Team, Age , Nat , Position ,From,To,Transfer fee
Gareth Bale, Real Madrid,24,Wales,RW,Tottenham,Real Madrid CF,91000000
Edinson Cavani,PSG,26,Uruguay,CF,SSC Napoli,PSG,64500000
Falcao, Monaco,27,Columbia,CF,Atlético Madrid,Monaco,60000000
Neymar, FC Barcelona,21,Brasil,LW,Santos,FC Barcelona,57100000
Mesut Özil, Arsenal,24,Germany,AM,Real Madrid ,Arsenal,50000000
James Rodríguez,Monaco, 21,Columbia,RW,FC Porto,Monaco,45000000
With this i want to skip the name and team but need age.
View 3 Replies
View Related
Mar 6, 2015
im trying to read employee information from a file using structures but im having problems with getting the file to open and read in the information
Code:
Write a programt that inputs (at most 50) records from a sequential file
#include <stdio.h>
#include <stdlib.h>
struct employee // employee structure definition
}
[code]....
View 13 Replies
View Related
Feb 3, 2013
i have a text file... with example following content:
Pizza 23232
Car 44,495
Drink 3493,90494
....
..
.
and so on..
no i want to find the 44,495 in this textfile.. calculate a new value like 44,495 x 2 + 5
and write the same file as
Pizza 23232
Car 93,99
Drink 3493,90494
....
..
.
back..
View 2 Replies
View Related
Feb 7, 2013
I know how to do this in c++ with fstream and std::string and getline and so on and so forth. Im writing my code solely in c however. I can't get g++ installed so figured it was a good excuse to learn c instead of using the equivalent c++ abstracts.
My problem is, I'm making a game in c that I have made in c++ but have ran into an issue with my map. I want to read in my map from a file which just looks like this:
Name of Town
* * * * * * *
* * * * * * *
* * * * * * *
etc...
so i tried using fscanf to first read in the name of the town (stored in a char*) then read in the characters (in this case '*')(not including white spaces becuase i can just print those) into another char*. what is the better way to do this?
View 16 Replies
View Related
Mar 27, 2014
So I am supposed to create a program that reads a file and replaces "<" with "<" , ">" with ">" , "&" with "&" , and " " " with """.........
The program takes a file "test.txt" and reads it, replaces the characters above with the corresponding strings, and then writes the output to "scrubbed.txt".
A sample input would be: <something>
and the output would be: <something>
for some reason I am getting garbage In my new file. What am I doing wrong with the code?
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char **argv){
char *inFileName = NULL;
char *outFileName = NULL;
FILE *instream = fopen("test.txt", "r");
FILE *outstream = fopen("scrubbed.txt", "w");
[Code] ....
View 5 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
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
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
May 3, 2013
I wrote the below code to find the line with "abc" as my parameter to strTosearch. I expected to the line 1st line but, this function always match the 2nd line. What am I missing here?
I wanted "found" to be "abc def hgi SSS".
Code in main()
String found=GetstringColSamLine("mytext.txt", "abc");
File name - mytext.txt
abc def hgi SSS
abc ppp yyy
string GetstringColSamLine(string routerFileName, string strTosearch)
[Code] ....
View 3 Replies
View Related
Aug 23, 2013
I am trying to use ini file for configuring my app.
But GetPrivateProfileString is not working.
I am using Windows 7. I tried "Wow64DisableWow64FsRedirection" also.
Not only this-ATLPath::FileExists is also not working.It gives an error code 0x80070002 which means:
"system could not find the file"
View 14 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
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