Hey guys,
I am working on a program which is supposed to read input from a file at 100 chars per time, then change the spaces to underscores and reverse the order of the entire document. Now we are not allowed to go over 100 input characters per time. I had the project finished until i realized that after 100 characters the program would print the incorrect part first.
for (i=0;i<n;i++)
{
if(buffer[i-1]== 32)
buffer[i-1]=95;
tempArray[n-i]=buffer[i-1];
flag=1;
}
this correctly changes the spaces to underscores and reverses the order of which they are inputted.
Here is my completely edited program which gets a segmentation error and I cannot get around it.
By the way it takes 3 files as input (which i am sure I dont need) and currently doesnt output anything right. I was attemping on using a dynamic array to fix this but I believe it keeps overwriting itself.
Any help I would be greatful.
-E
#include <stdio.h>
#include <sys/file.h>
#include <string.h>
int main(int argc, char* argv[])
{
int readFile, writeFile, intTemp, i, x, n, flag=0, count=100, factor=0,
times=0;
char buffer[100];
char tempArray[100];
char *stackIt;
char FILENAME[20] = "tempFile.txt";
int getSize(char *A)
{
return sizeof(A)/sizeof(char);
}
if (argc < 3)
{
printf("Error: An input and an output file name must be
provided!");
exit(0);
}
if (!strcmp(argv[1], argv[2]))
{
printf("Error: The files must have distinct names!");
exit(0);
}
readFile = open(argv[1], O_RDONLY);
if (readFile == -1)
{
printf("Error opening input file.");
exit(0);
}
writeFile = open(argv[2], O_CREAT | O_RDWR | O_APPEND, 0644);
int tempFile = open(argv[3], O_CREAT | O_RDWR | O_APPEND, 0644);
if (writeFile == -1)
{
printf("Error opening output file.");
exit(0);
}
n = read(readFile, buffer, 100);
while (n != 0)
{
if(flag==1)
{
for (i=0;i<n;i++)
{
stackIt[times] = tempArray[i];
times=times+1;
}
}
for (i=0;i<n;i++)
{
if(buffer[i-1]== 32)
buffer[i-1]=95;
tempArray[n-i]=buffer[i-1];
flag=1;
}
n = read(readFile, buffer, 100);
}
n = getSize(stackIt);
while(n != 0)
{
x = write(writeFile, stackIt, 100);
n=n-x;
}
n = write(1, "Program completed!\n", 19);
close(readFile);
close(writeFile);
exit(7);
}