C++ :: How To Display A String Per Line On TXT File
Mar 6, 2015
How can i display a string per line on the .txt i made? There are 2 .txt files, the one has: John Finn Xach The other is: password1 password2 password3 Question is: How can i display, let's say, i input "John" so it will display something like this "John's password is password1" or if i input Xach, "Xach's password is password3" When i use something like :
Code:
while(getline(passwordFile, str))
{cout<< username <<" password is " << str;
break;
}
It will work for when i input "John", but it doesn't work if I input "Finn" or "Xach"
EDIT: I tried this one:
Code:
while(getline(userFile, str1) && (passwordFile, str2))
{
cout<< str1 <<" password is " << str2;
break;
The output would be: Finn password is password1
It iterates the username but not the password.
View 7 Replies
ADVERTISEMENT
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
May 12, 2014
I have to read the information about logic gates from a file. The file format is shown below:
Code:
gateOne = NAND(inpA, inpB)
gate2=NAND(1,2)
3 = NAND(23,25,26)
As, it can be seen from the above structure that whitespaces are not same everytime. So, to deal with this situation, i am using boost library to remove all whitespaces from the line which is being read and then try to find the name of gate and its input. My code is given below which is able to correctly find the gate names and its first input...but my code is not able to find the second, third and so on input names.
Code:
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <boost/algorithm/string/erase.hpp>
[Code] ....
View 3 Replies
View Related
Jan 6, 2015
I am new in c programming. I want to write a function that will count the empty line of the file and display the content.
/* ***************************
** creator: firstprint *
******************************/
#include <stdio.h>
/* function that will identify if line is empty or not */
is_empty(char *buffer) {
while(isspace(*buffer)) buffer++;
if(*buffer==0x00) return 1;
[Code] ....
View 4 Replies
View Related
Mar 28, 2014
I'm making a game that requires the user to enter a correct username and password to play. I have a list of accepted ones saved in a txt file. How do i read each line of the file and check it with the one entered? I know how to use strcmp(string1, string2). I just need to know how to move from one line to the next. This is what I have for this part of the program so far. Also, I think im using the feof() wrong.
Code:
id = fopen("F://CS223/namepass.txt", "r");
score = fopen("F://CS223/scores.txt", "a");
srand(time(NULL));
if(id == NULL)
{
printf("ERROR");
[Code] .....
View 6 Replies
View Related
Mar 7, 2014
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
View 2 Replies
View Related
Jul 10, 2013
My program takes in an input file from the command line and converts the string from the file into a linked list and then depending on the command it will manipulate the string to either reverse the list, print the list, or take a character out...I'm having trouble taking a character out, my code compiles fine but doesn't change the string at all
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1024
[Code]....
View 4 Replies
View Related
Apr 19, 2014
I created class called students which suppose to store students names of in array but when I call the display function it display only the first name. but I want it to display names depending on the array size.
#include <iostream>
#include <sstream>
using namespace std;
const int SIZE=5;
[Code]....
View 3 Replies
View Related
Mar 17, 2013
I am having problems printing "->" on the beginning of each line, im trying to do it as a loop and ending it when the user types "q". also i would like to know how to ignore text from the user when the input begings with a specific character. heres what ive done so far, and not currrently working as expected.
Code:
#include <stdio.h>
int main (void) {
char prompt;
printf("~~~ FRACTION CALCULATOR ~~~
[Code] ....
View 1 Replies
View Related
Oct 21, 2014
Program to read in 15 numbers and display them as follows //each number on a separate line
Code:
# include <stdio.h>
# define MY_ARRAY 15
int main(){
int i ;
printf("please enter 15 numbers
[Code] ....
Error that i get from compiler:
Error E2062 array1.c 14: Invalid indirection in function main()
Error E2062 array1.c 19: Invalid indirection in function main()
both pointing to the separate "scanf"
View 6 Replies
View Related
Aug 17, 2014
I am trying to read a file line by line and then do something with the informations, so my method looks like this:
Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file.
");
exit(EXIT_FAILURE);
[Code] ....
1) The first complain of valgrind is at the line where I use fgets and its telling me (invalid write of size x), but I have allocated my line to 56000 and the read line is shorter, why is there a write size error then :S?
2) at the line where I realloc where I try to shrink the space he's telling me: Address .... is 0 bytes inside a block of size 56000, But I know i need only this space so why is there a write over space error :S??
View 9 Replies
View Related
Sep 25, 2013
So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4
The code should store each line in a list as such...
4, 3, 2 ,1
Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4
Here is my code...
struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}
View 4 Replies
View Related
Jul 15, 2013
I need to read lines from one file and copy them line by line into another file using dynamic memory allocation. It compiles but gives me a seg fault. Why/How?
Code:
int main(){
FILE *fp1;
FILE *fp2;
FILE *i;
fp1=fopen("file1","r");
fp2=fopen("file3","w+");
[Code] ....
View 2 Replies
View Related
Oct 5, 2013
I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...
I tried the code below but it will write from the second line, the whole line in one vector index.
int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
[Code]...
View 1 Replies
View Related
Apr 12, 2015
Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:
int main() {
string x;
ifstream fin;
int count = 0;
char ch;
fin.open("CWC_Master.txt");
if(!fin)
[Code] .....
Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:
superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00
Where it should output the above, instead it outputs every other one. Like:
superman toy
19.99
batman toy
25.99
aquaman toy
26.00
How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?
View 7 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
Oct 13, 2014
How to read a file line by line and then later access them by doing something like
Code:
lines[0] //Line number one
...
lines[100] //Line number one hundred and one
lines[100][0] //L
lines[100][1] //i
lines[100][2] //n
lines[100][3] //e
lines[100][4] //
lines[100][5] //n
...
View 13 Replies
View Related
Jul 2, 2013
I have an external file with one column of data. If I have a counter value let say counter =1, and counter++ and so on. How I can write such a c++ code that if the value of counter and value from the external file are same then generate an action let say cout both values i.e. value of counter and value from external file.
for more information, here is an example:
data in file(in one column): 2 6 8 9 10...
value of counter : 1 2 3 4 5 6 7 8 9...
then cout values only if value of counter and value from the file is same.
Here is my code so far, but it does not seem to work;
#include<iostream>
#include<fstream>
using namespace std;
int main() {
const int SIZE = 10; //Size declaration of array
int hours[SIZE]; //Array declaration
[Code]...
View 12 Replies
View Related
Jan 23, 2015
I am reading my file (20GB) line by line using boost like this
PHP Code:
boost::interprocess::file_mapping* fm = new boost::interprocess::file_mapping("E:Mountain.7z", boost::interprocess::read_only);
boost::interprocess::mapped_region* mr = new boost::interprocess::mapped_region(*fm, boost::interprocess::read_only);
char* bytes = static_cast<char*>(mr->get_address());
An exception is thrown in the second line while allocating memory for mr.
I use boost because it can also work in Mac which my code will be ported to.
View 3 Replies
View Related
Jul 27, 2013
First of all I need to know how to read from file.
Second, I need to read line by line from the file.
For example, the file contains:
8H 3S TH 9D 5S
5D KD 9H 4D 3D
8C TD 5D QS 2C
I wanna use array, so I need to store 8H in array[0], 3S in array[1], until 5S get stored in array[4].
I need to read the first line, the the second, and then the third.
View 3 Replies
View Related
Feb 3, 2013
I want to take multiple line string in c++. How can I take this.
Such as I want to take this as input:
HELLO MY LOVE, I M HAPPY BECAUSE SOON I WILL BE TO YOUR SIDE.
THIS TIME WITHOUT YOU HAS BEEN ETERNAL.
I INVITE YOU TO THE ZOO ONE TO SEE THE ZEBRAS AND GORILAS.
View 2 Replies
View Related
Sep 11, 2014
void login() {
//username, password, option to register, option to exit, link to website
cout << "Would you like to login(1), or press anything else to register.?" << endl;
cin >> loginYorN;
if (loginYorN == 1) {
cout << "Please enter your username: ";
cin >> accountInfo[1];
[code]...
So as above, I ask them to input 1 for login, anything else for register. Whatever they input, it get's stored into loginYorN, then I check to see what they input. If they put '1' for input, then I ask them for their username, store that into accountInfo[1], and then asking for the password, storing that into accountInfo[2].
Now here is where I need to input line 1(username) from login.csv or login.txt and line2(password) and storage these into accountID and accountPW.
View 1 Replies
View Related
Jun 14, 2013
It's been about two years since I last program c, now I need to do it for a basic project. How would I print out one letter in a string?
For example lets say I have a string called str=[Hello]. I want to display the third letter so just the "l". Here's what I have so far:
Code:
#include<stdio.h>
int main() {
char str[50] = "Hello";
printf("The third letter is : %s
",str[3]);
return 0;
}
View 2 Replies
View Related
Oct 28, 2014
Basically I am writing a program that will ask the user to input some names and then I will sort the names. However, right now when I input the characters, it prints out everything, including the empty spaces. How do I put an end of string mark at the end of the line, when the user hits enter
void main() {
constintMaxNames(20);
constintMaxLetters(15);
charNames[MaxNames + 1] [MaxLetters + 1];
intNumRows;
[Code] .....
View 1 Replies
View Related
Aug 4, 2014
I try to learn string class, resolving some problem, but i have some dificulties.The is ok, but when i print the longest string it does'n print on the same line.I enter the number of string, after that i enter the first string until i introduced from keyboard "#" character. I enter the second string and so on.Look at these example :
For i = 3;
Text[0] : I learn class String#
Text[1] : I dont learn class String#
Text[2] : String#
It print me like that : Text[1] :
I dont learn class String More than that look at the next example :
For i = 3;
Text[0] : I learn class String#abcdef
Text[1] : I dont learn class String#
Text[2] : String#
You see that in the first sentence i have continue to introduce some characters after # character and look what is happened :
Text[1] : abcdef
I dont learn class String
#include<iostream>
#include<string>
using namespace std;
int main() {
string text[100], cuvant;
int i, j, m, lung = 0;
cout << "
[code]....
View 3 Replies
View Related
Oct 7, 2012
I am working on a project were I have to read line form ( PLC ) programmable logic controller generated text file with lines like this
Circuit Value Current 2.33 4.32 5.55
there could be up to 3000 lines per txt file
I am using string stream to parse the line, for the sake of good programming I which to check weather first three values are string and last three values are actually floats raise or throw an exception if they are not ....
View 4 Replies
View Related