C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 6th, 2013, 03:48 PM
reed_pro reed_pro is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2013
Posts: 1 reed_pro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 12 sec
Reputation Power: 0
Unhappy Unwanted superscript 2 at end of file

I'm working on a program that wraps a text file to any desired length. It works fine, but it appends a superscript 2 to the end of the file

Does anyone know why this is happening? I appreciate your time

output:
wrapping to: 40
This is another file. It is depressingl
y short.

here is my code to wrap:
Code:
int wrap(char *fileName, int length)
{
	char *pBuffer = NULL;
	FILE *argFile = NULL;
	int i = 0;
	int lineLen = 0;
	int size = 0;
	//fseek(argFile, 0L, SEEK_END);
	//size = ftell(argFile);
	//fseek(argFile, 0L, SEEK_SET);
	//get size of file
	size = getSmallFileLength(fileName);
	//checks size for error
	if (size != errorNum)
	{
		//allocates memory
		if((pBuffer = (char *) malloc(sizeof(char) *size)) == NULL )
		{
				printf("There was a problem allocating memory");
				return fatalErr;
		}
		else
		{
			//opens file passed to the wrap function
			if ((argFile = fopen(fileName,"rb")) == NULL)
			{

				printf("ERROR: %s cannot be opened", fileName);
				return fileErr;
			}
			else
			{
				//reads file to buffer
				if ((fread(pBuffer, sizeof(char), size, argFile)) == 0)
				{
					printf("Error reading %s.", fileName);
					return fatalErr;
				}
				else
				{
					//closes file
					if(fclose(argFile) != 0)
					{
						printf("failed to close %s", fileName);
						return fatalErr;
					}
					//runs until end of buffer
					for(i = 0; i <= size && *(pBuffer + i) != EOF;i++)
					{
						//checks for newline charactor
						if (*(pBuffer + i) != '\n')
						{
							//checks to see if the length has been reached 
							if(((lineLen++ + 1) % length) == 0)
							{
								//if it has been reached, prints newline and character
								printf("\n%c", *(pBuffer + i));
							}
							else
							{
								//otherwise prints character
								printf("%c", *(pBuffer + i));
							}
						}
						else
						{
							//if there is a newline, reset lineLen to 0
							printf("%c", *(pBuffer + i));
							lineLen = 0;
						}
					}
					//frees memory after printing is completed
					free(pBuffer);
				}
			}
		}
		printf("\n");
	}
	else
	{
		printf("There was an error with the file name(%s)\n", fileName);
		return fileErr;
	}
	return 0;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Unwanted superscript 2 at end of file


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap