C++ :: Program That Reads Text From A File And Outputs Each Line To The Screen - I/O Streams
Jun 15, 2013
I seem to be missing a concept or 2 here ... I am tasked with writing a program that reads text from a file and outputs each line to the screen as well as to another file PRECEDED by a line number ...
In addition, I have to Print the line number at the start of the line and right-adjusted in a field of 3 spaces ...
Follow the line number with a colon then 1 space, then the text of the line.
Another kicker, is I have to grab the data 1 character at a time and write code to ignore leading blanks on each line.
Here is what I have so far:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
int main() {
char next;
int count = 0;
[Code] ....
View 7 Replies
ADVERTISEMENT
Apr 7, 2013
I have a text file where in each line is 3 words and 2 int type numbers. I'm doing search by word and I want to print out to the screen the whole line where that word was found (the words is repeated in some lines). My search looks like this:
Code:
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include <vector>
using namespace std;
int main(){
vector <string> words;
string str, paieska;
[Code] .....
How to print line(s) where I found that word?
View 9 Replies
View Related
Nov 8, 2014
I'm working on a program that reads in a .txt file and searches through the text for a keyword. If it gets a hit on the keyword, the line number where the keyword is located and the line that contains the keyword is printed out. What I have now doesn't catch every occurance of the keyword "a".
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
[Code]......
View 4 Replies
View Related
Feb 14, 2015
I am trying to make a simple login program that reads username and password from a text file.
In my text file I have:
name1;pass1
name2;pass2
name3;pass3
name4;pass4
name5;pass5
Everything is working okay with name1 and pass1, but if I try to log in with different credentials, for example name2 and pass2 it says "invalid details"
Here is my code:
string user, chuser;
string pass, chpass;
string los;
ifstream loginData("logindata.txt");
cout << "Please type in your username:" << endl;
getline(cin, user);
[code].....
View 5 Replies
View Related
Apr 3, 2015
I need to write a program that reads four float numbers from the input.txt file, then it prints out the greatest of the four numbers into the output.txt file. I did everything, but the numbers don't print out.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;
float number1, number2, number3, number4;
[Code]...
View 2 Replies
View Related
Apr 20, 2014
I need to make a program which reads multiple lines from a text file and stores that information in a vector of structs(the vector class template also needs to be custom made).
One of my requirements is to have a class dedicated for I/O for the text file. At the moment I can't seem to get a way to input the data from a file into a vector from an InputOutput class. This is my code:
InputOutput.h
#include <ostream>
#include <fstream>
#include "Share.h"
#include <string>
#include "Vector.h"
class InputOutput {
[Code] ....
This is the menu selection function in the menu.cpp where i figured i would call the Input file and store it from case 1. I think I'm doing it wrong though. Is there a better way of doing this because at the moment i am getting some errors such as error LNK2019.
void Menu::UserMenuSelection() {
Vector<Share> shares;
std::ifstream infile;
switch (menuOption) {
[Code] ....
View 2 Replies
View Related
May 10, 2013
This is a small program that reads a line from the screen, takes the ASCII values from the entered text, and edits it. Next, the ASCII values are reinterpreted (am i misspelling?) as text, and displayed. However, my final output has (allways?) at least 7 characters, even if you enter only one. I'm also trying to accomplish, that the part of the string which isn't filled, will be printed empty. In spaces.
compiled with -std=c99.
Code:
#include <stdio.h>
int main(){
int maxsize;
printf("How long is your message?
[Code] .....
View 6 Replies
View Related
Apr 30, 2015
I'm trying to make a program that reads data from a text document and allows me to modify it. I am stuck with the display() function. I can get the printf statement to display all my array values except the char AD value. When I include flight[i].AD it causes the program to crash. When I run the program to only display the AD variable I get a bunch of weird symbols. I'm not sure where the program is going wrong because it seems to be storing values properly except for the AD variable.
#include <fstream>
using namespace std;
//named constants
const int MAX=100; //maximum flights
const int SIZE=20; //maximum characters
//struct definition
struct FlightType
{
char name[SIZE];
[Code]...
View 1 Replies
View Related
Feb 6, 2014
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
//////////////////
View 2 Replies
View Related
Nov 18, 2013
I have been asked to write a program to grade several multiple-choice exams. The exam has less than 80 questions, each answered with a letter in the range of ‘a’ through ‘f’. The data are stored on several files such as exam1.dat where the first line is the key, consisting of a string of n characters (0<n < 80). The remaining lines on the file are exam answers, and consist of a student ID number, a space, and a string of n characters.
The program should have a while loop in the main routine to ask users input a data file name through keyboard. In this way, the program has a capability of repeatedly asking for a new data file in each while loop until users input a key word “exit”. Once “exit” is encountered, the while loop terminates and your program ends. A typical input exam data file (exam1.dat) looks like:
abcdefabcdefabcdefab
1234567 abcdefabcdefabcdefab
9876543 abddefbbbdefcbcdefac
5554446 abcdefabcdefabcdef
4445556 abcdefabcdefabcdefabcd
Apply strlen( ) or the length( ) of string to the first line of the above data file for determining the number of questions in the problem. If a student gives more answers than necessary, the extra answers will be automatically truncated. On the other hand, if a student provides less number of answers, the remaining unanswered questions are considered as being answered wrongly.
After users input an exam data file, your program should ask users to input another grade-curving file name (e.g., gradeCurving.dat). This second file contains the information to convert a percentile score to a curved grade in levels of ‘A’ through ‘E’. For instance, a grade-curving file takes the following format: a curved alphabetic grade, a space, a percentile grade served as marker.
A 90
B 80
C 70
D 60
E 50
The above information means that ‘A’ = 90 through 100; ‘B’=80 through 89; ‘C’=70 through 79; ‘D’ = 60 through 69; “E”=50 through 59; For the remaining grades, you can assign an ‘F’.
Furthermore, in each while loop in the main routine, your program should ask users to input an output file name such as score1.dat. The output file will store the scores for each student in a format: student ID number, a space, a percentile score, and a curved grade in ‘A’ though ‘E’. The program should also compute and display the following statistics for the graded answers: Average score, Maximum score, and Minimum score.
A typical output on a data file looks like:
1234567 90% A
9876543 85% B
5554446 95% A
4445556 75% C
5551112 80% B
Statistics:
Average Score: 85%
Minimum Score: 95%
Maximum Score: 75%
This Is what I have so far. It compiles fine and everything but when I input the files it says "There was an error opening the corresponding files. Check input name perhaps?" and it exits out ....
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <assert.h>
using namespace std;
int openfiles(ifstream& infile, ifstream& curvingfile, ofstream& outfile);
void Size(ofstream&,int,string);
[Code] ....
View 11 Replies
View Related
Jan 21, 2015
I wrote the following program:
#include "stdafx.h"
#include "string.h"
#include "ctype.h"
[Code].....
View 1 Replies
View Related
Jul 5, 2013
I have a text file (test.txt) with the following data:
01,05,25,20130728
01,06,25,20130728
01,07,25,20130728
01,08,25,20130728
01,05,25,20130728
01,05,25,20130728
01,05,45,20130728
01,05,65,20130728
01,05,85,20130728
01,05,35,20130728
01,05,25,20130728
01,05,35,20130728
I want to read this to one string called line. So far I have this code:
string line;
ifstream myfile ("/home/Test.txt");
if (myfile.is_open()) {
while (myfile.good()) {
getline (myfile, line);
for (int a = 0; a <= 335; a++) {
cout <<line.at(a);
} }
myfile.close();
}
so far its only printing the first line and then throwing an instance of 'std::out_of_range'
View 2 Replies
View Related
Aug 24, 2013
I can able to get the last line ten chars but i cant get if the last line is null.
here is my code
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
[Code] ....
here with i have enclosed my file where i am pick my last ten chars ...
Attached Files : log.txt (4.8 KB)
View 1 Replies
View Related
May 6, 2014
I need to do a code that gave me Original string, uppercase string, lowercase string, reverse string (if letter is upper then convert to lower, and if lower then convert it to upper) and uppercase first (first character of each word in uppercase).
I need to do it in functions but i dont know hot to use strings. The program should provide the option to save the outputs in a file.
View 4 Replies
View Related
Jan 16, 2014
I've been working on a program that displays the number of digits in each line of a file, but I feel stuck. Take for example a file that contains these characters:
6347aaa9
54j
811111
6a
709
And I'm trying to display a result like this
1 //that's the number of the line 5 //the number of digits
2 2
3 6
4 1
5 3
Here's what I've written so far:
Code:
#include<stdio.h>
int main() {
char a=0;
int number_of_digits=0, linescount=0, num, number_of_digits_per_line=0;
FILE *inputFile;
if(!(inputFile=fopen("C:TestTest.txt","r")))
[Code]..
I also thought of using fgets and strlen but I am not very good with them and couldn't get the program to work correctly. It did work but it displayed all characters, letters included, not only digits.
View 2 Replies
View Related
Feb 26, 2013
Write a program that reads data from a file (use the attached data file). These data are a student name and 3 test scores. The program should calculate the average of the 3 test scores, and display the name, 3 test scores, and the average to the monitor.
Useful tips:
a) Include the following header files: iostream, fstream, iomanip, and string
b) The name of the data file is “datafile.txt”, you need to save the file in the same folder of the source file.
c) use the manipulators (setw, setprecision, setfill, showpoint, fixed) to format the average with 1 digits after decimal point as following.
d) Use character ‘ ’ for tab.
View 3 Replies
View Related
Mar 7, 2013
I am trying to write a program for class that reads from an input file the month, year & total collected. I am using notepad for the input file.
This is what the notepad file looks like
-----------------------------------
March 2013 63924.62
Why does it give me random numbers in the output rather than what the notepad has?
Also, the outfile is completely blank.
#include <iostream>
#include <iomanip>
#include <fstream>
[Code].....
View 2 Replies
View Related
Sep 30, 2013
For class I need to write a program that inputs a file (the dividend), performs binary division on the file (using 0x12 as the divisor), and outputs the remainder(checksum).
I have researched binary division algorithms and I get the general gist, but I'm still unsure where to start. How would I store the dividend and divisor? As two arrays of bits?
Then, I need to figure out how to perform shifts and XORs on the the binary numbers. Maybe I should use bitwise operations?
View 5 Replies
View Related
Jan 19, 2014
I am working on a wage application with four options which are as follows:
Subtracts an amount from the shop bank account (The account is text file "shop"). You do not need to add the same amount to another account but you should record the transaction with a timestamp in a separate file. The application should prevent the account balance from becoming overdrawn.
List the five most recent transactions to the screen. If there haven’t yet been five transactions then list all of them.
Print the account name, number and current balance to the screen.
Exit the program.
I have completed 1,3 and 4 but I am completely stumped as to how to go about number 2.
#include <limits>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
int read_balance(void);
void write_balance(int balance);
[Code] .....
View 1 Replies
View Related
Mar 13, 2014
[URL] .....
Here is my code and basically these are the steps. I feel like we have something good to work on but we keep getting errors.
a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.
b. Save the output of the program in a file.
c. Modify the function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends
the number read to the function main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers.
e. Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum and average of the numbers.
View 12 Replies
View Related
Sep 14, 2014
I wanted to know if i could use two streams with a file
fstream fS;
ifstream read;
fs.open("text.txt", ios::in | ios::out | ios::app);
read.open("text.txt", ios:: in);
my aim is to read a record from text.txt in to fs and compare it with other records of text.txt by read
bool checkID(char userID, inStream read);
read.open("text.txt", ios::in);
while (read.good()){
read.getline(id, size);
if(strcmp (id, userID)==0)
return true;
}
return false;
View 8 Replies
View Related
Jun 11, 2014
I have a .txt file which I want to read from and then write a new text file, this time with sorted lines. It is easy to sort one value, but what about sorting entire lines based on one value?
I want to sort the lines based on the FIRST value.
Example text file contents:
values;data
5.01 100
2.9 342
2.69 43534
10.1 43
6.8 45324
Output: Text file containing
2.69 43534
2.9 342
5.01 100
6.8 45324
10.1 43
It's easy to do this if there was only 1 number on each line, but I do I sort all the lines based on the first number in each line?
View 2 Replies
View Related
Jun 7, 2013
How can i delete a line from a file created using a C++ program?Can i get a code sample.
View 1 Replies
View Related
Jan 29, 2015
The program i wrote can run but did not work. I want to delete a line from the textfile "StaffList.txt" but when i cin the name, for example "Ben". After the program ran finished and i open the textfile, the name "Ben" and the whole line is still inside the textfile! I have created the "temp.txt" file before i run the program.
//information inside the "StaffList.txt"
James S1325685J12580
Peter S8856325K52650
Ben S1025639Z85250
Ken S9052365B74100
Marry S9352654I75320
John S7754852H99630
Jenny S7042525L74530
May S1256985G15960
Betty S1135565K56360
Kyle S9545530A1234100
[code]....
View 2 Replies
View Related
Feb 9, 2014
I need to store the last 3 deposit that I have in my deposit. I have few option but i dont which would be easy
1.Store all deposit to the text file (store always in a new line), and display the last three deposit from the text file.
dep 1 - 60
dep 2 - 40
dep 3 - 100 print 100
dep 4 - 50 print 50
dep 5 - 50 print 50
2. I think this option is more difficult, when it reach deposit 4, to get rid deposit 1
So when i make a deposit 4, the deposit 1 get's ride
dep 2 - 50
dep 3 - 100
dep 4 - 70
Right now i can only display one deposit (last one), then i close the program and run again and i make another deposit it overwrites a new deposit.
My code.
To show sure my deposit that has been made.
double depSave () {
int option;
float dep1,dep2,dep3;// Declare your variables
dep1 = dep2 = dep3 = 0; // Set them all to 0
system ("cls");
string path = "deposit.txt"; // Storing your filename in a string
[Code] ....
Where is says "save deposit" in comment that where it saves to the deposit text file, that going to be output to the depSave function.
View 2 Replies
View Related
Aug 16, 2013
I am making a script to read the latest from a text file. It picks up the line by numbytes in fseek, but the data line may vary and numbytes not be accurate, how can I fix this?
And another problem is that the line has, date, time, value, separated by space, how to read that line and put these 3 information in variable?
#include <stdio.h>
#include <conio.h>
int main() {
FILE *arq;
char Line[100];
char *result;
int tam, i;
// Opens a file for READING TEXT
arq = fopen("temp.txt", "rt");
[code].....
View 19 Replies
View Related