The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
I/O files allocating memory and print out charcters with while loops
Discuss I/O files allocating memory and print out charcters with while loops in the C Programming forum on Dev Shed. I/O files allocating memory and print out charcters with while loops C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 25th, 2004, 05:09 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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: ~>
|

November 25th, 2004, 05:16 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
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.
|

November 25th, 2004, 11:44 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 2
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|