View Full Version : making a program to combine multiple text files and to replace multiple spaces(C)
alvinleephd
12-09-2006, 09:35 PM
here's what i have..
#include <stdio.h>
int main() {
char c[100], d[100], e[100];
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r");
if(file==NULL)
else if (file2==NULL)
else if (file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(c, 100, file)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while(fgets(d, 100, file)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 100, file)!=NULL) {
strcat (c, d, e);
printf("With all the files combined, the output is: %s \n", c);
fopen("file1.txt", "w");
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
it's not working as i would like it to, but i don't find much wrong with my algorithm. unless i have a few errors in syntax..
i then have to write a function that will find multiple spaces in the string (between each word).
i have a general idea on finding A space, but i don't know how to find MULTIPLE spaces and how to identify them from a string. and also i have no idea how to replace those multiple spaces with a single space.
any help would be appreciated!
if(file==NULL)
else if (file2==NULL)
else if (file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
...
Question; Why do you have empty if and else if clauses? Did you mean...
if(file==NULL || file2 == NULL || file3 == NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else
{
....
I know this doesn't really help you with your problem, I just thought I'd mention it.
alvinleephd
12-09-2006, 10:00 PM
yeah, i just reviewed conditional statements and saw that. thanks, though.
alvinleephd
12-09-2006, 10:01 PM
also, strcat doesn't seem to work. does strcat only work for up to two strings?
Ok, I felt my above post didn't help any so I'll add some more thoughts.
What I would do is;
1. Read a character,
1a. If it's a letter/number etc, print it to the screen/file, read next character.
1b. If it's whitespace don't print anything. Then keep reading characters, but don't print until you get one that isn't whitespace. Once you've found a character that's not whitespace, print a single "space" and continue on.
There's a method in <ctype.h> called isspace(char c)). It'll take a character and tellsyou if it's whitespace or not. This includes spaces, tabs etc.
The following is a method I wrote in C++ that strips all whitespace (spaces and tabs etc) from a string. Obviously you won't be able to use it line for line as there aren't Strings in C and you're also using files, but it demonstrates the algoithm I laid out above.
char c;
const string& data = this->getStringData(); //String of contents of a file. No strings in c though...
//Remove all non-alpha characters
//For loop from 0 to the length of the string, you'll need to modify...while(not EOF) or something
for(unsigned int i = 0; i < this->dataSize(); i++)
{
c = data[i]; //Gets the next character, you'll need to modify
//If character is a letter/number and isn't a "space" and isn't whitespace, save/print it
if(isalpha(c) && c != '\n' && !isspace(c))
{
this->alphaData().push_back(c);
}
}
Here's a useful link of methods in ctype.h http://www-ccs.ucsd.edu/c/ctype.html
EDIT: Yes, as far as I'm aware strcat takes only 2 char*'s as parameters. http://www.cplusplus.com/ref/cstring/strcat.html
alvinleephd
12-09-2006, 10:19 PM
thanks, that makes sense!
also my question about strcat.. since it doesn't work for anything more than 2 strings, what could i substitute it with?
Well if you know that you will always have 3 files then you could just use strcat twice.
//strcat(char* destination, char* source)
strcat(c, d); //concat's c and d and saves as c, now concat with e
strcat(c, e)
alvinleephd
12-09-2006, 10:44 PM
alright revised code: #include <stdio.h>
int main() {
char c[50], d[50], e[50];
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r");
if(file==NULL || file2==NULL || file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(c, 100, file)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while(fgets(d, 100, file)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 100, file)!=NULL) {
strcat (c, d);
strcat (c, e);
printf("With all the files combined, the output is: %s \n", c);
fopen("file1.txt", "w");
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
any idea why it won't compile? it says "syntax error at end of input", although i can't find any
alvinleephd
12-10-2006, 12:42 AM
ok i came up with this based on your algorithm, but it doesn't seem to work:
#include <stdio.h>
int main() {
char c[50], d[50], e[50];
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r");
if(file==NULL || file2==NULL || file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(c, 100, file)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while(fgets(d, 100, file2)!=NULL) {
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 100, file3)!=NULL) {
strcat (c, d);
strcat (c, e);
if (c[i] = %' '|| c[i] = %d || c[i] = %c)
printf("With all the files combined, the output is: %s \n", c);
fopen("file1.txt", "w");
else
printf("\n");
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
it also has problems compiling
Ok, in brief there are many issues with what you just posted, I'll list some of them to get you started.
1. you call strcat, but you never include the file that tells the compiler how strcat is defined. You'll need to add #include <string.h>
2. You have 3 nested while loops that have opening braces "{", but not closing braces "}". I had to add a 3 "}"'s at the end of your code just to get it to compile (along with other fixes).
3. You have a variable i, that is never declared. I assume this i appeared based on one of my previous posts, so you'll need to declare i or rethink your algorithm.
4. c[i] = %d and c[i] = %c: All I can say for this one is, huh? I'm not sure what you're trying to do here but it's not legal syntax that's for sure. Even if i was declare, a) you're using an assignment statement in a condition; change =, to ==. But the %c and %d is where the real issue is since it's not proper syntax and this is what makes me say "huh"?
Start by fixing those and get it to compile. Then take note of what the output is (I think it won't be what you're after) and go from there.
My advise especially with C is to building your program incrementally. Write a few lines of code and compile to make sure it's working as expected. Then write a few more lines and compile again...repeat this process until you have a fully working program.
Good Luck.
alvinleephd
12-10-2006, 01:46 AM
errr, i'm totally confused.
well i tried to make a few corrections, but now i only have more errors
#include <stdio.h>
#include <string.h>
int main() {
char c[50], d[50], e[50];
int i;
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r");
if(file==NULL || file2==NULL || file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(c, 100, file)!=NULL){
printf("File 2 opened successfully. Contents:\n\n");
while(fgets(d, 100, file2)!=NULL){
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 100, file3)!=NULL){
strcat (c, d);
strcat (c, e);
if (c[i] ==' '|| c[i] >= 'a' && c[i] <= 'z' || c[i] >= '1' && c[i] <= '9 ')
printf("With all the files combined, the output is: %s \n", c);
fopen("file1.txt", "w");
else
printf("\n");
}
printf("\n\nNow closing file...\n");
fclose(file);
return 0;
}
}
}
}
}
i changed the IF statement, i just don't know what to put there for it to look for characters and integers. (to determine whether they are, too)
Ok, so your method of trying to combine my suggestings with your previous algorithm aren't working so I think we need to pick one of the other. I pick my way ;)
#include <stdio.h>
#include <string.h> //Needed for strcat
#include <ctype.h> //Needed for isspace()
int main() {
char c[50], d[50], e[50];
char ch;
int counter = 0;
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r");
if(file==NULL || file2==NULL || file3==NULL)
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
//file1
ch = fgetc(file); //Read a character from the file
while(ch != EOF) //We'll keep reading characters until the End Of the File is reached.
{
if(!isspace(ch)) //If the character isn't whitespace we add it to the array
{
c[counter] = ch; //Add to the array
counter++; //Increment to next spot in the array
}
else //Character read was whitespace
{
ch = fgetc(file); //Get the next character in the file
while(isspace(ch) && ch != EOF) //Keep reading characters until we get one that isn't whitespace
{
ch = fgetc(file);
}
if(ch != EOF) //We got a character that wasn't whitespace and we're not at the end of the file.
{
c[counter] = ' '; //We want one space not many, so add a single space
counter++; //Increment to the next spot in the array
c[counter] = ch; //Add the character that was read that comes after the whitespace
counter++; //Increment to next spot in array
}
}
ch = fgetc(file); //Read the next character in the file
}
c[counter] = ' '; //Reached the end of the file, add a single space so that there's a space between file1 and file2
c[counter+1] = '\0'; //Make sure the array ends with \0 (all strings i.e. char []'s must end with \0
printf("Now closing file1...\n");
fclose(file); //close the file
...Do the same for file2 and file3...
...Then do your strcat as you had it...
strcat(c, d)
strcat(c, e)
printf("Contents of combined files: %s \n", c);
You should noticed that thins code segment I posted strips out any multiple occurrances of spaces from file1.txt. You'll need the same for the other two files and then use strcat.
Thins you need to be aware:
1. You're limited to the total of 50 characters since you declare c[50]. If there's more than 50 characters in the combined files or any file on its own, you'll get a segmentation fault.
2. There was something else I wanted to mention, but I've forgetten...
alvinleephd
12-10-2006, 02:31 AM
alright, makes sense now!
Yes it is, I talked about it a few posts ealier.
Here's another link to help convince you... http://www-ccs.ucsd.edu/c/lib_over.html
alright, makes sense now!
You're not just saying that are you? It's worth your while to make sure that you do understand so that life is easier in the future. If you have more questions, please ask.
alvinleephd
12-10-2006, 02:58 AM
i get the general idea, and i'm trying to implement it for file 1 and 2.
however, i'm trying to modify it to work kind of differently.
i want the function to replace the spaces in the RESULTING file of the strcat.
alvinleephd
12-10-2006, 03:11 AM
would something like this work:
#include <stdio.h>
#include <string.h> /* Need this library for strcat to work */
#include <ctype.h> /* needed for isspace, found it in the book */
int main() {
char c[50], d[50], e[50]; /* declaring strings for each file, up to 50 values*/
char ch;
int counter = 0; /* declaring counter so that i can add and increment to spots the character array/string */
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r"); /* declaration of files, opening of files(or creation if they don't exist) */
if(file == NULL || file2 == NULL || file3 == NULL) /* if NULL, which means broken or invalid, it will not open the file and it will end the program */
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(c, 50, file)!=NULL){ /* gets contents from the file if it is not broken */
printf("File 2 opened successfully. Contents:\n\n");
while(fgets(d, 50, file2)!=NULL){
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 50, file3)!=NULL){
strcat (c, d);
strcat (c, e); /* strcat to combine all 3 strings. first combine the first two, then the result of the first two with the last one, e */
printf("With all the files combined, the output is: %s \n", c);
fopen("file1.txt", "w");
/* replacing multiple spaces */
ch = fgetc(file); /* reads a character from the file*/
while(ch != EOF) /* loop makes it so that it will read the characters until the END of file is reached */
{
if(!isspace(ch)) /* if the character is not a white space, it is added to the array*/
{
c[counter] = ch; /* adds to the array*/
counter++; /*increments to next spot in the array */
}
else /* if character was read as a white space */
{
ch = fgetc(file); /* get next character in the file*/
while(isspace(ch) && ch != EOF) /* Keep reading until there is one that isn't white */
{
ch = fgetc(file);
}
if(ch != EOF) /* if we get a character that wasn't a white space and we're not at the end of the file */
{
c[counter] = ' '; /* adds a single space */
counter++; /* increment to next spot in array */
c[counter] = ch; /* add the character that was read that comes after the whitespace */
counter++; //Increment to next spot in array
}
}
ch = fgetc(file); /* reads next character in the file */
}
c[counter] = ' ';
c[counter+1] = '\0';
printf("Now closing file1...\n");
fclose(file);
strcat(c, d);
strcat(c, e);
printf("Contents of combined files: %s \n", c);
return 0;
}
}
}
Well, the short answer is no. How do I know that? Because it doesn't even compile.
I had to add 3 "}" 's to your file just for it to compile...
Make sure that you close all the files, right now you're only closing one of them.
Next, please explain your logic behind the 3 nested while loops. It doesn't make any sense to me, nor do I think it will work.
Here's we go once more. I have altered your code which results in a closer match to what you're after. However, it doesn't work perfectly.
include <stdio.h>
#include <string.h> /* Need this library for strcat to work */
#include <ctype.h> /* needed for isspace, found it in the book */
int main() {
char c[50], d[50], e[50]; /* declaring strings for each file, up to 50 values*/
char ch;
int counter = 0; /* declaring counter so that i can add and increment to spots the character array/string */
FILE *file, *file2, *file3;
file = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
file3 = fopen("file3.txt", "r"); /* declaration of files, opening of files(or creation if they don't exist) */
if(file == NULL || file2 == NULL || file3 == NULL) /* if NULL, which means broken or invalid, it will not open the file and it will end the program */
{
printf("ERROR: CANNOT OPEN FILE.\n");
return 1;
}
else {
while(fgets(c, 50, file)!= NULL){ /* gets contents from the file if it is not broken */
printf("File 1 opened successfully. Contents:\n\n");
while(fgets(d, 50, file2)!=NULL){
printf("File 2 opened successfully. Contents:\n\n");
while (fgets(e, 50, file3)!=NULL){
strcat (c, d);
strcat (c, e); /* strcat to combine all 3 strings. first combine the first two, then the result of the first two with the last one, e */
printf("With all the files combined, the output is: %s \n", c);
fclose(file);
fopen("file1.txt", "w");
/* replacing multiple spaces */
ch = c[counter]; /* reads a character from the file*/
while(ch != '\0') /* loop makes it so that it will read the characters until the END of file is reached */
{
if(!isspace(ch)) /* if the character is not a white space, it is added to the array*/
{
fputc(ch, file); /* adds to the array*/
counter++; /*increments to next spot in the array */
}
else /* if character was read as a white space */
{
ch = c[counter]; /* get next character in the file*/
while(isspace(ch) && ch != '\0') /* Keep reading until there is one that isn't white */
{
counter++;
ch = c[counter];
}
if(ch != '\0') /* if we get a character that wasn't a white space and we're not at the end of the file */
{
fputc(' ', file); /* adds a single space */
fputc(ch, file); /* add the character that was read that comes after the whitespace */
counter++; //Increment to next spot in array
}
}
ch = c[counter]; /* reads next character in the file */
}
c[counter] = ' ';
c[counter+1] = '\0';
//fputs(c, file);
printf("Now closing file1...\n");
fclose(file);
fclose(file2);
fclose(file3);
strcat(c, d);
strcat(c, e);
printf("Contents of combined files: %s \n", c);
return 0;
}
}
}
}
}
In your while loop for stripping out the spaces, you were reading again from file1. What you wanted to do was read each character in c (which is the concatenation of all files) and strip the spaces out of that. Then, you want to add those characaters to file1.
As I stated above, I've fixed your code so that it does mostly what you want, but the end contents of file1 is missing some characters. My guess is there is a counter problem in the while loop.
EDIT: I've found the the extra counter increment that was causing issues. I've edited your code above, and I think it now works as you'd intended.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.