Printing Garbage

Hi all, I am trying to reorganize a list that I downloaded from the web. I am loading / reading in one char (ch) at a time. Then using
a group of functions to determine the type of the loaded char. If it is alpha, or space do nothing other than let me know.
If it is a digit or punct, open a file and write / append it to the file. The program does open and write to the file. . . but, it just
prints garbage. Where are my errors?
This is what I get from the file:
1
2
3
㈴㐠‱‭‭㈴㐠‰ㄴ㐠′ㄴ㐠‱〴㬠ⰠⰠ⸠㐠‰㔴⼠㐠‱㠴⼠㐠‱㈴㐠′‭ㄴ㐠‶‭㈴㐠″‭㌴㐠″ㄴ㐠‵ㄴ㐠‱ ㈴㐠‹ ㄴ㐠‱㐴ⴠ㐠‱㠴ⴠ㐠′㐴ⴠ㐠′㘴㐠‱〴
㐠‰㘴⼠㐠‱〴⼠㐠‱ㄴ㐠‸‭㈴㐠‸‭㌴㐠″‭㐴㐠′ㄴ㐠‸〴㐠‵ ㄴ㐠‵ ㄴ㐠′㈴㐠‱‭㈴㐠‴‭㌴㐠‴‭㐴㐠″ㄴ㐠‰ㄴ㐠‱ ㈴㐠‵ ㄴ㐠‱㠴ⴠ㐠″㐴ⴠ㐠″
㤴ⴠ㐠‴㌴㐠‱ㄴ㐠‰㘴⼠㐠‰ 


I don't know how much of my code you need, so I'll give you all of it. I hope its not too much. Thanks for the help!
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
						// isalpha, is digit, isspace, ispunct test program
#include <stdio.h>		// printf
#include <ctype.h>		// isalpha isdigit, isspace etc
#include <iostream>		// cout, cin
#include <fstream>
#include <string>
#include <ios>			// ios::app
#include <stdlib.h>

using namespace std;

#define FALSE 0
#define TRUE  1

/* function declarations   */
int char_type(char);
char m;
//int ch;
//int ch;
int AlphaResult = 0;
int Isdigit = 0;
int Isspace = 0;
int Ispunct = 0;

int main()
{
	char k;
	char ch;

	//Opens Temp_Program for reading the file

	ifstream a_file ( "C:\\tmci\\CodeTesting\\RawData.txt" );
	if (!a_file )
	{
		cout << "Can't open input file" << " RawData.txt " << endl << endl;
		cin.get();
		exit(1);
	}

	for( int i = 0; a_file; i++)
	{
		a_file >> ch;		// reads a character from the file RawData.txt

		//char_type:decides the character type
		int char_type(char ch);
		{
			/* returns non zero if A-Z or a-z  */
			AlphaResult = isalpha(ch);

			if ( isalpha(ch) != FALSE)
			{
				printf("%c is an Alpha character.\n",ch);

				//cin.get();
			}

			/* returns non zero if 0-9    */

			if ( isdigit(ch) != FALSE)
			{
				ofstream b_file ( "C:\\tmci\\CodeTesting\\SortedMMList.txt", ios::app );

				if (!b_file )
				{
					cout << "Can't open input file " << " SortedMMList.txt " << endl;
					cin.get();
					exit(1);
				}

				Isdigit = isdigit(ch);

				b_file << Isdigit << ch << " " ;

				//printf("%c is a numeric character.\n",ch);

				int Isdigit = 0;

				ch = 0;

				cin.get();
			}

			/* returns non zero if a space, CR, Tab, NL FF  */
			Isspace = isspace(ch);
			if ( isspace(ch) != FALSE)
			{
				printf("%c is white space\n", ch);
			}

			/* returns non zero if a punciation ( '.' or ',' or ':' or ';' ect.*/

			if ( ispunct(ch) != FALSE)
			{
				ofstream b_file ( "C:\\tmci\\CodeTesting\\SortedMMList.txt", ios::app );

			if (!b_file )
			{
				cout << "Can't open input file " << " SortedMMList.txt " << endl;
				cin.get();
				exit(1);
			}

				Ispunct = ispunct(ch);

				b_file << ch << " " ;

				printf("%c is punctions\n", ch);

				Ispunct = 0;

				ch = 0;

				//cin.get();
			}
		}
	}

	cout << "This is the end of program" << endl << endl << " Press any key to continue";
	cin.get();
	//cin.get();

	return 0;
}


I've tries everything I know without success. Googling was no help.
Have u tried debugging it with breakpoints,keep a watch on variables, that would help you to isolate your problem.
Last edited on
closed account (zb0S216C)
Was the file written in binary mode? If so, use binary mode.

Wazzak
Last edited on
 
isalpha(ch) != FALSE


is equivalent to

 
isalpha(ch) == TRUE


which is equivalent to
 
isalpha(ch)


Not the source of your problem, just wanted to throw that one out.
Yes I have placed a *cin.get()* after all the

1
2
3
4
5
6
7
b_file << ch << " " ;   // writes to output file

printf("%c is punctions\n", ch); // lets me know the type

Ispunct = 0; // resets Isdigit to rid garbage

ch = 0;  // resets ch to rid garbage  


which is after all the print / write statements. That did not help. I even placed a statement like

Ispunct = ispunct(ch);

to find out what was in *ch* All that did was to return a number. I don't know how to have the program take a char from the input file ( RawData.txt ) and print it in the output file ( SortedMMList.txt ).

I've tries everything I know without success
Please review functions and scope.
You can't define a function inside another function.

Your code is quite obfuscated, based in your description
1
2
3
while(std::cin>>character)
  if( isdigit(ch) or ispunct(ch) )
    std::cout << ch;
ness555, Please explain. . . I am newbie to c++ and you just spoke Greek to me, sorry.
¿what you don't understand?
You can't define a function inside another function.
Where did I do this?

Your code is quite obfuscated, based in your description
Please explain?

Also, I don't understand your:
1
2
3
1 while(std::cin>>character)
2 if( isdigit(ch) or ispunct(ch) )
3 std::cout << ch;

I understand a very little about a while loop and even less about passing parameters (std::cin>>character).
I recognize cin as waiting for a char from the keyboard, but not when the input char must come from a file.

I understand that the:
1
2
if( isdigit(ch) or ispunct(ch) )
std::cout << ch;

is waiting input from keyboard, but not if input is from either a file or a program.
Sorry to be such a newbie. . . but I'm learning.
A: You can't define a function inside another function.
1: Where did I do this?
A: You didn't, because you can't.
Look at line 45, ¿what are you trying to do there?
It looks that you wanted to provide the body of the function. To do that it should be
1
2
3
4
5
6
7
8
int main(){
/* program */
}

//outside main
int char_type(char){ //defining the function
//...
}



Your code is obfuscated. It is hard to follow, you've got a lot of global variables, you assign them values but never use them, have local variables with the same name,
embed error code, handling the same error multiple times (the opening of the file), embed debug code,
repeated code, bad indentation, etc.


So based in your description is the suggested code. It shouldn't bother that I've used std::cin instead of a file, they are both streams and the behaviour should be the same.
Besides you could redirect the standard input to a file
By instance $ ./program.bin < input.txt, instead of reading from the keyboard it will read from the file

Oh, didn't realize that there was a typo
1
2
3
while(std::cin>>ch) //while you can read a character
  if( isdigit(ch) or ispunct(ch) ) //if the character is "valid"
    std::cout << ch; //output it 

Wow. I never knew about $ ./program.bin < input.txt. Where can I find the documentation for this. . . I couldn't find it with Google?

Would that then be:?
1
2
3
while($ ./program.bin < input.txt) //while you can read a character
  if( isdigit(ch)) //if the character is "valid"
   b_file << ch; //output it  


Could you explain just how / where this could work? . . . . thanks!
$ ./program.bin < input.txt

Would be the program invocation from the command line.

Your file appears to contain unicode. Treat it as a unicode file.
Topic archived. No new replies allowed.