how do I ensure that the encoding is ascii??

closed account (iw0XoG1T)
I am reading one file and saving it in another file one character at a time. If I cout the character to the console I see what I want to save in a file as ascii characters, but I am unable to make the conversion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void copy_by_line(ifstream *ist, ofstream *ost)
{
	vector<char>empty;
	vector<char>temp;
	while(*ist)
	{
		for(int i=0; i<163; ++i)
		{
			temp.push_back(ist->get());
		}
		for( int i =0; i<temp.size(); ++i)
		{
			*ost << temp[i]; // these characters are not always ascii
			cout << temp[i]; // I want to save what I see here as an ascii character.
		}
		*ost << endl;
		temp = empty;
	}
}
I don't think I understand what you're asking.
closed account (iw0XoG1T)
Here is the output to a file:
1,Clamp,12.48,Workbench clamp
਍㔀 Ⰰ䘀氀愀琀 䠀攀愀搀 匀挀爀攀眀搀爀椀瘀攀爀Ⰰ㌀⸀㄀㜀Ⰰ䘀氀愀琀 栀攀愀搀ഀഀ
75,Tire ൂ
ar,NULL,Tool for changing tires.
਍㌀   Ⰰ㌀洀洀 䈀爀愀挀欀攀琀Ⰰ ⸀㔀㈀Ⰰ一唀䰀䰀ഀഀ
￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿਍

Here is what it looks like in the source file which is also how it looks when I output it to cout:
1,Clamp,12.48,Workbench clamp
50,Flat Head Screwdriver,3.17,Flat head
75,Tire Bar,NULL,Tool for changing tires.
3000,3mm Bracket,0.52,NULL


edit:
Adding more clarification to my problem in hope of an answer. I have a very large csv file which I am unable to use. So I played with some test data and found that if I open the file in microsoft wordpad and save it as a text file I am able to use it. My problem is the actual file is too large to open so I am trying to write a utility program to produce this same result. What you see in my post above is a guess that is not working.
Last edited on
I'm not sure if this has anything to do with it or not but line 9 is calling get() repeatedly without checking for errors. Is the data really exactly 163 bytes?
closed account (iw0XoG1T)
re-wrote based on moorecm comment:
1
2
3
4
5
6
7
8
9
10
11
12
13
void copy_by_line(ifstream *ist, ofstream *ost)
{
	vector<char>temp;
	int i=0;
	while(*ist)
	{
			temp.push_back(ist->get());
			*ost << temp[i];
			cout << temp[i];
			++i;
		
	}
}

here is the new output:
1,Clamp,12.48,Workbench clamp
਍㔀 Ⰰ䘀氀愀琀 䠀攀愀搀 匀挀爀攀眀搀爀椀瘀攀爀Ⰰ㌀⸀㄀㜀Ⰰ䘀氀愀琀 栀攀愀搀ഀഀ
75,Tire Bar,NULL,Tool for changing tires.
਍㌀   Ⰰ㌀洀洀 䈀爀愀挀欀攀琀Ⰰ ⸀㔀㈀Ⰰ一唀䰀䰀ഀഀ
closed account (iw0XoG1T)
If you care this seemed to work:
1
2
3
4
5
6
7
8
9
10
11
12
void copy_by_line(ifstream *ist, ofstream *ost)
{
	char temp = 0;
	while(*ist)
	{
		temp = ist->get();
		if(isalnum(temp) || ispunct(temp) || temp == '\n' || temp == ' ')
		{
			*ost << temp;
		}
	}
}


I still do not completely understand what the problem was; I understand my solution because all I did was filter out what I could not use.
Last edited on
Topic archived. No new replies allowed.