String array

I am trying to write a file that reads from a file, prints the file on the screen, and prints the info into a second file. This is the code I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>										
#include <string>										
#include <fstream>										
#include <stdio.h>										
#include <ctype.h>

using namespace std;

int main()
{
    char curLetter, str[5000];													
	int a, i, len, Opt;

	a=0, Opt=0;

	len=strlen(str);

	ifstream infile;
	ofstream outfile;
	infile.open("c:\\B4Enc.txt");
	outfile.open("c:\\AEnc.txt");

	if (!infile)										//error msg if no file is found
		cout << "NO INPUT FILE!!!" << endl;

	while ((str[a++]=infile.get()) != EOF)a;
	{
		for (i=0; i < len; ++i)
		{
			curLetter = (str[i]);
			cout << curLetter;
			outfile << curLetter << endl;
		}
	}

	infile.close();
	outfile.close();
	
	return 0;
}


The output looks like this:
Hi. How are you today? I am good, Thank you. ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ WITH A LOT OF THESE ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╤kYÅ¿ ↕Press any key to
continue . . .


I am getting what I want to print out on the screen, and the file but with all the extra stuff (you know what I mean "╠╠╠╠╠╠╠╠╠".

Any thoughts of what I need to do to get rid of it? Thanks for any and all helpp!!
Simplify your code :) Also you have a rogue 'a' at the end of your while definition.

1
2
3
4
5
6
7
8
9
vector<string> vLinesList;
string sLine = "";

ifstream fInputFile("C:\\Meh.txt");

while (getline(fInputFile, sLine)) {
 vLinesList.push_back(sLine);
 cout << sLine << endl;
}
How does that reduce the erronous output?
You've taken a very odd approach to reading the file. But the actual cause of your problem is: for (i=0; i < len; ++i). len is 5000, so it's going to print 5000 chars everytime. Or perhaps a random amount until a null char is found.


Last edited on
how can i tell it to only print the chars in the file if i dont know how many are in the file?
Last edited on
You can either count them as you read them, or, if you are reading and writing one at a time, why do you care how many there are?
The conclusion we have as of now is that my char str[5000] is the reason why it is printing the "╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠" inaddition with"╤kYÅ¿ ↕" at the end of the run. I am trying to elminate them. So we have concluded that it is reading nothing after it runs out of text and is pringint "╠". I want to stop printing the "╠". How can I tell it to stop printing when it is done with usable text?
Last edited on
i think you have to also check inside the loop if you finished reading the file.
and whats the a; at the end for??
while ((str[a++]=infile.get()) != EOF)a;//<<<<-------
OK, by reducing the str[5000] to 500, it reduces the errounous output. How do i make it go away alltogether?
It's because your array of chars, when initialized, contains garbage data. So when you get only 50 chars, the first 50 chars are good, but the rest (garbage data) still exists, which is printing fine. I suggest you use an std::string if you want to do it this way.
Can you post your modified version of the code please.
not sure what u mean with a std::string? can u elaoborate pls
Instead of
char str[5000];
use
std::string str;
(if you are using namespace std you don't need the std::)
if you want to use a char array then have the first char in the array = \0;
and when ur outputting check to see if your at the \0. if you are then exit(or you can just it in the for loop condition)

1
2
3
4
 char str[5000] = {0}; // make an array all set to null.

 // after each use to clear it again
 memset(str, 0, sizeof(str));
Topic archived. No new replies allowed.