C++ :: Reading Two Words Into Single Char Array From Text File

Sep 15, 2014

I am trying to read in player names (ex: first last) from a text file into the people[].name data struct. I can successfully read in my card file, but I cannot get this to work. I get a seg fault. I believe this is because nothing is actually being read in for my while loops. I can't use std::strings so these must be c-style strings aka char arrays.

// deck of cards
// below are initializations
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;
//globals
const int maxCards = 52;

[Code] .....

View 1 Replies


ADVERTISEMENT

C++ :: Reading In Text File Into Char Array

Oct 17, 2014

I have a project which is about linked lists in the order of: a Story is made of Paragraphs which is made of Sentences which is made of Words. We must treat Words specifically as character arrays, not strings. So I need to read in a story from a text file and make Words by finding the first whitespace/punctuation(everything before the whitespace up until the whitespace or punctuation is the Word), all the Words up to the punctuation are a Sentence (Sentence is a linked list of Words), Paragraph is all the Sentences up until an empty line, and a Story is all the Paragraphs. So I know doing this will let me take a line and put it into a character array:

char charArray[25]; //we are allowed to assume a word won't be longer than 25 characters
int i = 0;
ifstream myFile(fileName.c_str());
if(myFile.is_open()){

[Code].....

I suppose I could look at charArray[i] and if it is ws or punctuation then make the Word = charArray[i-1], but is there an if statement I could do that would prevent the ws or punctuation from being read into charArray in the first place? Because a problem I see with the charArray[i-1] method already is that "This is a story." would get put into the array as Thisisastory. and thus I'd be unable to break up at a space.

So to summarize: I want to read in a text file character by character into a char array, which I can set to length 25 due to context of the project. I want to read in each character and at a whitespace or punctuation, I want to take everything already in charArray and feed that into a Word object constructor (the next Word gets linked to the previous Word, and at a punctuation all the Words linked together become one Sentence, and all the Sentences linked before an empty line become a Paragraph, etc). So how can I get charArray to be only characters in a word, then after the word being read-in ends, charArray resets to empty, and then is populated by the next word and so on.

View 2 Replies View Related

C++ :: Concatenate Two Char Arrays Into Single Char Array?

Sep 29, 2014

I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.

/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit

[Code]....

View 2 Replies View Related

C/C++ :: Compare Words From Text File With Array?

Apr 16, 2014

I need to compare the words from a text file with words from an array (which i got from another file) and count the instances of those words. I thought about making another array but that doesn't make sense since the text file may have a lot of words in it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
FILE *myFile;
char words[100][40], test;

[code]...

the test variable in the last for loop is wrong i know i was just trying something.

View 13 Replies View Related

C/C++ :: Reading Words From Text

Dec 31, 2014

This what I'm trying to do:

Type two or more lines of text into a file. Write a program to open the file, reading each word into a vector<string> object. Iterate over the vector, displaying it to cout. That done, sort the words using the sort() generic algorithm

But I'm stuck unfortunately.

#include <iostream>
#include<fstream>
#include<vector>
#include<cstdio>
using namespace std;
int main() {
ifstream infile("about.txt");

[Code] .....

Error: 'about' was not declared in this scope

View 1 Replies View Related

C/C++ :: Reading Strings To Char Array From Exe File

Feb 25, 2014

I am trying to read strings to an char array from an .exe file and then i would check some of the strings, but the problem is that the only thing that is read from the file is the first string (MZ) and an 'square' that is some incorrect character. I am using fread to read from the file. Here is my code:

FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen("my_file.exe", "rb");
if( pFile == NULL) exit(1);
fseek(pFile,0, SEEK_END);

[Code] ....

(I want to read the whole file, its not that big)...

View 14 Replies View Related

C++ :: Compare Element Of Char Array And String With Single Character

Dec 3, 2014

how can i compare an element of the char array and string with single chsracter also how to compare char array to cpp string

View 3 Replies View Related

C++ :: Reading Int From Text File With 2D Array

Nov 3, 2014

Reading in a maze into a 2D array. The first two reads will give me the dimension of the maze(ex. m x n maze). So in order to create the 2D array i need the first two reads. Then after it is created it will read the rest of the data which are 1s and 0s. I have to create a program that will solve the maze but i cant test my movement code if i cant read in the data first. the entire program compiles but to test if i read the file i have a function to print it. But it says "There are: 0 rows and 0 columns " so it didn't read anything since rCount and cCount are initialized to 0. and basically the maze has nothing in it.

Code:
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdlib>
#include "position.h"

[Code] .....

View 3 Replies View Related

C++ :: Reading Text File Into Array?

Apr 17, 2015

I have to take a text file with 62 8 25 7 90 82 22 46 15 54 in it. The array has to read from the file. I cannot figure out how to do it. This is what I have.

Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
ifstream FileIn;
ofstream FileOut;

[Code] ....

View 2 Replies View Related

C++ :: Store Contents Of Text File Into Char Array?

Apr 22, 2014

I am trying to store the contents of a text file into a char array. However the function i am using ifstream member function get(); seems to stop working when fed with certain characters. Is there another solution besides the get() function that will accept all types of characters from files?

char text[1000];
for (int i = 0; i <= textlen; ++i)
{
text[i] = text_in.get();
}

View 3 Replies View Related

Visual C++ :: Reading Into Array / 2d Array From Mixed Text File?

Jan 18, 2014

I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60

ex. text file

JES
DAN
JEN
.
.
.
01010101
10010101
RAM
JET
01010010
10100101
.... and so on

should i use a while loop?

View 10 Replies View Related

C++ :: Reading Words From A File

Mar 3, 2013

I'm trying extract words from a text file and then put then them into a set. I want to delimit all the spaces as well as the periods. My code is working fine for spaces but its leaving out the periods at the end of some words.

void Dict::get_words(string file){
ifstream file(file);
int end, beginning = 0;
string word;
string line;

[Code] .....

View 3 Replies View Related

C++ :: Dynamic Memory (array Of Char) - Delete All White Spaces In Text File

Feb 8, 2014

General Purpose: Delete all "white spaces" in text file, until the read-in char is _not_ a whitespace (mark as start of text file).

Problem: Cannot seem to shift char's over properly. (I think my problem is the inner loop - however other code may lead to this problem - I do not know)

Code:

#include <fstream>
#include <iostream>
using namespace std;

bool trimWhiteSpace(fstream &file, char * charMemoryBlock) {
if (!file.is_open()) {

[Code] ....

View 3 Replies View Related

C++ :: Storing Words In TXT File And Reading Them?

Apr 12, 2013

I would like to store the titles of a CD and then read them. I have started a program but not sure how to display or make sure it is storing it in the .txt file.

#include <iostream>
#include <fstream>
#include <cstdlib>

[Code]....

View 3 Replies View Related

C++ :: Read Text File Char By Char By Using Vector Class

Oct 29, 2014

Code:

cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())

[Code] .....

View 8 Replies View Related

C++ :: Forming Unique String With Single Occurrence Of Words

May 10, 2012

I am trying to form unique string from other string which is having multiple presence of same word as,

string testA = "one
two
three
four
one
seven wo";

now new string should contain single occurrence of words "one" and "two" so that new string will look like,

string testB = "one
two
three
four
seven"

View 3 Replies View Related

C :: How To Select Words From A Text File

Oct 1, 2013

I've a text file with 9 words and each word is written in a line. for exemple this is the list :

APPLE
ORANGE
MOON
CAR
CANDY
...
END

I'm gonna choose the fourth line and select the word that is in this line and work on. I tried lot of way but no result.

View 7 Replies View Related

C/C++ :: Getting Data From File - Count Number Of Words In A Text File

Mar 27, 2014

I have a program I have to do that counts the number of words in a text file. I have tried the code on 2 computers now since my programming teacher told me the code was fine. Here is my code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("tj.text" , ios::in);

[Code] .....

View 4 Replies View Related

C++ :: Comparing Words With Contents Of Text File

May 23, 2013

I want the user to enter the word and I want to check if it exists in a text file or not. I tried

int flag=0;
char word[50], input[50];
cout<<"Enter word to search for";
gets(input);

[Code] ....

But it does not seem to be working. Should I make changes to it or is there another way?

View 6 Replies View Related

C :: Read Text From A File And Show What Words Occur How Many Times

Apr 21, 2013

I'm trying to make program that will read text from a file and show what words occur how many times. I'm trying to make it so that if a new string comes, it saves it into wlist[], and counts 1 to wcount[]. If the same word is there, it will just add 1 to wcount[]. The program will end when it gets to the end of a file.

I don't know the reason, but I can't get strcmp and strcpy to work. I had to put a pointer in curword, just to make it compile.

Code:
#include <stdio.h>
#include <string.h>
#define LIST_MAX 100
#define WORD_MAX 20
int main(){

[Code] ....

View 6 Replies View Related

C++ :: How To Pick One Of Words Randomly From Text Instead Of Using All Words In It

May 19, 2013

I was reading this earlier [URL] ..... and I was trying to figure out how to pick one of the words randomly from my text instead of using all the words in it.

View 4 Replies View Related

C :: Copy Every Single Character From One Char Into Another

Jun 5, 2013

I have some problems with copying a single character from one char into another. How do I do that? Here is the function I am using

Code:

void
text_col (char line[]) {
char line1[1];
int l, ii;
l = strlen(line);

[Code] ....

I am trying to copy in a loop every single character from char line into char line1. That is not working.

View 8 Replies View Related

C :: Reading From Text File

Mar 11, 2013

extracting records from a text file based on the user input of a specific date. Below is a image of the Text file with Dates and records that follow. a code to extract records from file based on the day the user enters with month and year not necessary here.so it is based on the day the user enters, which the code will extract the records and if day is not found then "DATE NOT FOUND"

View 12 Replies View Related

C :: Reading Text From A File

Mar 12, 2013

I'm writing a code which has to read a text from a file. In this text, there are some symbol and numbers (ex, This is $19 of....). Those symbols and numbers should be replaced with the words from the other file. i.e.

transformed file

This is just an $19 of what the $5 should obtain.

And $19 should be replaced from association file

19 example
5 program

The result:

This is just example of what the program should obtain. I'd like to save the first file content into an array of strings and the same for the text of second file.

View 2 Replies View Related

C++ :: Reading From A Text File

May 9, 2013

I got an assignment to do...and in that i need to get the inputs from a .txt file....and im not getting how to read the data i want and skip the rest thrash.

Lambton:

Name: chocolate
demand rate: 50
setup cost: 200
unit cost: 5.2
inventory cost: 2

[Code] ....

Program is about a coffee shop with two branches and i have to keep track of the stock in the stores.

Lambton and Callaghan are the stores.

How can i read them ? I know how to open the file to be read..

View 2 Replies View Related

C :: Visual Studio 2012 - Writing Data Into TXT File / How To Stop Reading Char

Feb 23, 2013

I am using visual studio 2012.....in below code i m writing data in to a test.txt file but i dont know with which key file stop accepting char...i tried ctrl+z and ctrl+d but not working ....

Code:
#include<stdio.h>
#include<conio.h>
main(){
char ch;
FILE *ptr;

[Code] ....

View 7 Replies View Related







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