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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 25th, 2004, 05:09 PM
thefirm thefirm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 thefirm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question I/O files allocating memory and print out charcters with while loops

What this program does is allows you to enter the filename at the command prompt for the file that will be used to read information from, in additon to allowing you to enter the filename at the command prompt for a file that will write information to. THe only problem I am incountering is only reading in one line when there is 3 strings of charaters and numbers

HERE IS THE CODE
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main(int argc, char *argv[])
{
FILE *input, *output;
int i=0, m=0;
char character;
char *string;

if ((input = fopen(argv[1], "r")) == NULL)
{
if (argv[1]!=NULL)
{
printf("The file \"%s\" seems to be missing, so exiting.\n", argv
[1]);
exit(EXIT_FAILURE);
}
printf("Can't open an undefined file for reading, so exiting.\n");
exit(EXIT_FAILURE);
}

if ((output = fopen(argv[2], "w")) == NULL)
{
if (argv[2]!=NULL)
{
printf("Can't define the file \"%s\" for writing, so exiting.\n",argv[2]);
exit(EXIT_FAILURE);
}
printf("Can't open an undefined file for writing, so exiting.\n");
exit(EXIT_FAILURE);
}


while((character=fgetc(input))!=EOF)
{

/* Allocate an initial memory size for string */
string = (char *)malloc(2*sizeof(char));

/* If a NULL pointer is returned, display a warning! */
if (string == NULL)
{
printf("Could not allocate memory. Aborting...\n");
exit(EXIT_FAILURE);
}

while((*(string+i)=fgetc(input))!='\n')
{

/* Re-allocate memory for string */
string = (char*)realloc(string,i*sizeof(char)+2*sizeof(char));
i++;
/* If a NULL pointer is returned, display a warning! */
if (string==NULL)
{
printf("Memory re-allocation failed! Aborting...\n");
exit(EXIT_FAILURE);
}

}
printf("%s\n",string);



/* Put the '\0' character in the last element of the "array" */
*(string+i)='\0';
i++;

/* Print the string to the output file */
fprintf(output,"%s\n",string);
printf("Size of the array: %i\n",i);

/* Free up the memory since we are done with it */
free(string);

}

/* End program */
return(0);

fclose(input);
fclose(output);
return(0);
}

WHAT I GET WHEN I COMPILE IT AND RUN IT:

TRITON:gcc hw23a.c
TRITON:a.out input.txt output.txt
i my name is Dr. Some

Size of the array: 22

Size of the array: 25

Size of the array: 33
TRITON:cat output.txt
i my name is Dr. Someon


TRITON:

WHEN I SHOULD BE GETTING IS:

triton: ~> gcc hw23.c -o hw23.o
triton: ~> hw23.o input.txt output.txt
Size of memory stored for string 1: 22
Size of memory stored for string 2: 2
Size of memory stored for string 3: 10
triton: ~> cat output.txt
Output String: My name is Dr. Some
Output String: c
Output String: asdf 35 a
triton: ~>

Reply With Quote
  #2  
Old November 25th, 2004, 05:16 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,509 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 17 h 53 m 58 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
Please read the post for new users to learn how to put your code in tags to preserve its indentation. The post contains a number of tips, makes a nice read.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

Reply With Quote
  #3  
Old November 25th, 2004, 11:44 PM
thefirm thefirm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 2 thefirm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm using Putty as a Telnet and SSH for Win32 and Unix platforms so is basic C programming in my case

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > I/O files allocating memory and print out charcters with while loops

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 - 2013, Jelsoft Enterprises Ltd.

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