Thread: Problems with file i/o

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Problems with file i/o

    I've been working on this HTML case conversion program for about an hour now, but i'm stumped.

    Code:
    fileIn.open(g_pcInputFile, ios::in);
    fileOut.open(g_pcOutputFile, ios::out | ios::trunc);
    
    if (fileIn.fail())
    {
    
    	cout << "\tFailed to open input file, please ensure path and filename are correct.\n";
    	getch();
    	return 1;
    
    }
    
    
    do {
    
    	char pszBuffer[512];
    	fileIn.getline(pszBuffer, 511, '\n');
    
    	char *pszTag = strchr(pszBuffer, '<');
    	while (pszTag != NULL)
    	{
    		cout << pszTag << endl << endl << endl;
    		for (int i = 0; i < strlen(pszTag) + 1; i++)
    		{
    			if (pszTag&#91;i] != '>')
    			{
    				if (g_bGoingUpper)
    				{
    					pszTag&#91;i] = toupper(pszTag&#91;i]);
    				} else {
    					pszTag&#91;i] = tolower(pszTag&#91;i]);
    				}
    
    			} else {
    				break;
    			}
    		}
    		cout << pszTag << endl << endl << endl;
    		pszTag = strchr(pszBuffer + (pszTag - pszBuffer + 1), '<');
    	}
    //	cout << pszTag << endl << endl << endl;
    	fileOut.write(pszBuffer, 512);
    
    } while (!fileIn.eof());
    
    fileIn.close();
    fileOut.close();
    This is the main program loop. However, all that gets outputted to the fileOut is a bunch of garbage. On top of that, the test for fileIn.eof() doesn't seem to be working, as the program loops forever and fills fileOut with endless crap. I probably have to flush something somewhere, but I'm not too up on this subject.

    Basically, I open an HTML file, read in a line, find the tags, convert them to either uppercase or lowercase, output it to the output file and repeat. I did this in MFC using the CString and CFile classes, but I lost the source to that

    Edit:: Heh, subscripting with variable i confuses the board
    Last edited by Dual-Catfish; 07-02-2002 at 09:53 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fileOut.write(pszBuffer, 512);

    I would probably use:
    fileOut << pszBuffer << endl;
    here.

    But if using write, then:
    fileOut.write(pszBuffer, strlen(pszBuffer));

    I have no clue about the endless loop. It sounds like the getline() is failing at some point. You might check for fileIn.fail().

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I think .eof() checks is EOF has been read. Therefore, you have to make the input before you call that function.

    Code:
    char pszBuffer[512];//don't want this declared every time
    fileIn.getline(pszBuffer, 511, '\n');
    
    do {
    	char *pszTag = strchr(pszBuffer, '<');
    	while (pszTag != NULL)
    	{
    		cout << pszTag << endl << endl << endl;
    		for (int i = 0; i < strlen(pszTag) + 1; i++)
    		{
    			if (pszTag[i] != '>')
    			{
    				if (g_bGoingUpper)
    				{
    					pszTag[i] = toupper(pszTag[i]);
    				} else {
    					pszTag[i] = tolower(pszTag[i]);
    				}
    
    			} else {
    				break;
    			}
    		}
    		cout << pszTag << endl << endl << endl;
    		pszTag = strchr(pszBuffer + (pszTag - pszBuffer + 1), '<');
    	}
    //	cout << pszTag << endl << endl << endl;
    	fileOut.write(pszBuffer, 512);
                    fileIn.getline(pszBuffer, 511, '\n');//move getline call to bottom
    } while (!fileIn.eof());
    
    fileIn.close();
    fileOut.close();
    Tell me how that works.

    EDIT: Sorry, the spacing didn't work out too well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM