5 Replies - 508 Views - Last Post: 25 February 2017 - 01:41 PM Rate Topic: -----

#1 Mavs25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 17

[Newbie] Reading a text file into a char of an array?

Posted 21 February 2017 - 10:44 PM

Hi all! I was wondering if you guys could help me on my code, I am trying to read a text file into a array of chars. I am trying to use getline(inputFile, message[length]) but it is saying it's an instance overload. I would like to use an array of chars instead of using string because in the fixCapitalization function, I am trying to iterate so that every after a certain "." or "space", it will capitalize the letter after it. Any will help, thank you!

The part where I'm having trouble the most
//Read the file
		for (int i = 0; i < message[length]; i++)
		{
			getline(inputFile, message[length]);
		}
		




#ifndef MESSAGE_H_
#define MESSAGE_H_


#include <iostream>
#include "Message.h"
#include <string>
#include <fstream>
#include <string>
using namespace std;


class Message
{
private:
	char *message;   // holds the message
	int length;  // holds the the message length
	static const short ALPHABET_SIZE = 26;
	char code[ALPHABET_SIZE]; // holds the cypher alphabet
							  // iztohndbeqrkglmacsvwfuypjx
							  // ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth

							  // returns the input file size in bytes

streamsize getFileSize(fstream &file) const
	{
		streamsize fsize = 0;
		file.seekg(0,ios::end);
		fsize = file.tellg();
		file.seekg(0,ios::beg); // moves file pointer back to the beginning
		return fsize;
	}
public:
	/*
	* This constructor tries to open the file whose name is passed
	* to it in filename. If file opens successfully, calls function
	* getFileSize to determine how many bytes should be allocated
	* for the message. Allocates space for message and reads the
	* content from the file into it. Closes the file at the end.
	* Member variable length should be set to the file size.
	* If file cannot be found, length should be set to zero.
	*/
	Message(string messageFile)
	{
		
		//Open the message file
		fstream inputFile;
		inputFile.open("Encrypted.txt");

		//Check if the input file has successfully open
		while (inputFile.fail())
		{
			cout << "Input file has failed to open." << endl;
		}

		//Call function getFileSize and read into an array of message
		length = getFileSize(inputFile);
		char *message = new char;
		*message = message[length];

		//Read the file
		for (int i = 0; i < message[length]; i++)
		{
			getline(inputFile, message[length]);
		}
		
		//Check to see if the file was read successfully
		for (int i = 0; i < message[length]; i++)
		{
			cout << message[i] << endl;
		} 

		
	}

	/* The destructor frees the space allocated to message
	virtual ~Message();

	// Decodes the message
	void decode();

	// Capitalizes first letter in each sentence
	void fixCapitalization();

	// Prints the content of message on the screen
	void dump() const;

	// Returns true if the message is empty
	bool isEmpty() const;
	*/
};

#endif /* MESSAGE_H_ */


int main()
{
	

	/*//Create a message object with the content of Encrypted.txt
	Message m("Encrypted.txt");


	//Check if the fie was succesfully opened
	if (m.isEmpty())
	{
		cout << "Could not read message. Make sure the file has been implemented correctly in the code or the location "
		 << "is in the same folder." << endl;
		return EXIT_FAILURE;
	}

	//Cout the original message
	cout << "Original message: " << std::endl;
	m.dump(); //Function that couts the original message
	cout << endl << endl;

	//Call the function that decodes the message
	m.decode();

	//Call the function that fixes the capitalization of the function
	m.fixCapitalization();
	
	//Call the function that prints the doceded message
	std::cout << "Decoded message: " << std::endl;
	m.dump();

	cout << endl << endl;

	return EXIT_SUCCESS;
	*/

	system("pause");
}





Is This A Good Question/Topic? 0
  • +

Replies To: [Newbie] Reading a text file into a char of an array?

#2 Mavs25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 17

Re: [Newbie] Reading a text file into a char of an array?

Posted 21 February 2017 - 10:58 PM

Sorry for the double post, I'm not sure where the edit is or if I can even edit my original post but, the file "Decrypted.txt"'s contents is

ifqkwxcadf ar cei fpoi masif cd cei xkdqirr du pxxnwafm pf pnmdkaceo cd p oirrpmi, teaqe rqkpohnir cei gpcp af ac-oplafm ac sikw gauuaqvnc pfg caoi qdfrvoafm, au fdc xkpqcaqpnnw aoxdrrahni, cd gigvqi cei dkamafpn masif dfnw cei ifqdgig gpcp. afxvcr cd cei pnmdkaceo cwxaqpnnw afsdnsi pggacadfpn riqkic gpcp qpnnig liwr, teaqe xkisifcr cei oirrpmi ukdo hiafm giqdgig-isif au cei pnmdkaceo ar xvhnaqnw lfdtf.



Also, please let me know if I also made any other mistakes.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: [Newbie] Reading a text file into a char of an array?

Posted 22 February 2017 - 12:52 AM

Think about it..

//Read the file
		for (int i = 0; i < message[length]; i++)
		{
			getline(inputFile, message[length]);
		}
		



Your array is called message your length of the array is called length.

to access a single value of the array its message[someValue]

Now do you want a loop that goes from 0 to whatever is stored at message[length] which is accessing a index which is out of bounds ie array sizes go from 0 to length-1 or do you simply want the length ??
Was This Post Helpful? 0
  • +
  • -

#4 Thomas1965  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 276
  • Joined: 09-September 16

Re: [Newbie] Reading a text file into a char of an array?

Posted 22 February 2017 - 03:22 AM

I would like to use an array of chars instead of using string because in the fixCapitalization function, I am trying to iterate so that every after a certain "." or "space", it will capitalize the letter after it.
You can easily do it with a string.

With a string you could read your file very easy like this:
const string src_name = "names.txt";
ifstream src(src_name.c_str());
if (!src)
  // handle error

string buffer, line;
while(getline(src, line)
  buffer += line;

// use buffer


This post has been edited by Thomas1965: 22 February 2017 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: [Newbie] Reading a text file into a char of an array?

Posted 22 February 2017 - 09:43 AM

A couple of quick points:

First look at this snippet:
	char *message;   // holds the message
	int length;  // holds the the message length
	static const short ALPHABET_SIZE = 26;
	char code[ALPHABET_SIZE]; // holds the cypher alphabet
							  // iztohndbeqrkglmacsvwfuypjx
							  // ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth

							  // returns the input file size in bytes

streamsize getFileSize(fstream &file) const
	{
		streamsize fsize = 0;
		file.seekg(0,ios::end);
		fsize = file.tellg();
		file.seekg(0,ios::beg); // moves file pointer back to the beginning
		return fsize;
	}


Now since you use "length" to store the return value from getFileSize() it should really be a streamsize type not an int.

Why are you using a char* instead of a std::string? The same for your "code" variable ist really should be a const std::string and then you wouldn't need ALPHABET_SIZE since a std::string knows it's size. Also if you use a std::string getFileSize() would be unnecessary as well.

Now this snippet:

		//Open the message file
		fstream inputFile;
		inputFile.open("Encrypted.txt");

		//Check if the input file has successfully open
		while (inputFile.fail())
		{
			cout << "Input file has failed to open." << endl;
		}

		//Call function getFileSize and read into an array of message
		length = getFileSize(inputFile);
		char *message = new char;
		*message = message[length];


If the stream is in a fail state why are you continuing the program? If the file failed to open you probably should return to the operating system instead of trying to read a failed stream which will fail.

Do you really want message to be a single char? Again if you used a std::string you wouldn't need to try to use (misuse) new. Perhaps you should find and study some documentation for creating a dynamically allocated array in C++ using new.

Now this:
		for (int i = 0; i < message[length]; i++)
		{
			getline(inputFile, message[length]);
		}

Your for() statement is incorrect (a for() loop is really the wrong choice for this loop). First trying to access message[length], where length is the size of your array, is accessing the "array" out of bounds. Second do you realize that getline() retrieves a whole line of code and your length represents the number of characters in your file, not the number of lines. So if you're trying to retrieve length lines the getline() will probably fail before getting close to length. In the case of your sample input it would fail on the second call to getline() because the sample file you posted only has one line. Also note that that version of getline() is for a std::string, not a single character or an array of char.


I really recommend you stick with a std::string instead of trying to use C-strings.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Mavs25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-February 17

Re: [Newbie] Reading a text file into a char of an array?

Posted 25 February 2017 - 01:41 PM

Thank you everyone! I decided to switch to string like you all said and everything is working in order. Thank you all for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1