Reading from two different files and printing to third one











up vote
3
down vote

favorite












I need to read from 2 different files and print words to third one, first i read from file1 and save first word to file third, then read from file2 and save first word from here to third file as second word and so on.. and also words from file1 have "+" at the start of the word and from file2 have "-". My problem is that code doesnt stop and is still working and printig to third file "."(lot of dots). thanks



#include<stdio.h>
#include<stdlib.h>

int main(){
FILE *first, *second, *third;
char ch[256],ch1[256],ch2;
int i=1,count;
char space = ' ';
char minus = '-';
char plus = '+';

first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");

if(first==NULL || second==NULL || third==NULL)
{
printf("error");
exit(1);
}

while (fscanf(first, "%255s", ch) == 1)
{
count++;
}

while (fscanf(second, "%255s", ch) == 1)
{
count++;
}

printf("%d",count);

for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}
fputc(space,third);
}


fclose(first);
fclose(second);
fclose(third);

return 0;
}









share|improve this question






















  • count is uninitialized
    – yano
    Nov 12 at 17:38












  • yes, but thats not the biggest problem. thanks
    – Rudolph
    Nov 12 at 17:39












  • Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
    – Antti Haapala
    Nov 12 at 17:41






  • 1




    using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
    – yano
    Nov 12 at 17:42






  • 1




    fgetc returns an int.
    – Antti Haapala
    Nov 12 at 17:43















up vote
3
down vote

favorite












I need to read from 2 different files and print words to third one, first i read from file1 and save first word to file third, then read from file2 and save first word from here to third file as second word and so on.. and also words from file1 have "+" at the start of the word and from file2 have "-". My problem is that code doesnt stop and is still working and printig to third file "."(lot of dots). thanks



#include<stdio.h>
#include<stdlib.h>

int main(){
FILE *first, *second, *third;
char ch[256],ch1[256],ch2;
int i=1,count;
char space = ' ';
char minus = '-';
char plus = '+';

first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");

if(first==NULL || second==NULL || third==NULL)
{
printf("error");
exit(1);
}

while (fscanf(first, "%255s", ch) == 1)
{
count++;
}

while (fscanf(second, "%255s", ch) == 1)
{
count++;
}

printf("%d",count);

for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}
fputc(space,third);
}


fclose(first);
fclose(second);
fclose(third);

return 0;
}









share|improve this question






















  • count is uninitialized
    – yano
    Nov 12 at 17:38












  • yes, but thats not the biggest problem. thanks
    – Rudolph
    Nov 12 at 17:39












  • Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
    – Antti Haapala
    Nov 12 at 17:41






  • 1




    using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
    – yano
    Nov 12 at 17:42






  • 1




    fgetc returns an int.
    – Antti Haapala
    Nov 12 at 17:43













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I need to read from 2 different files and print words to third one, first i read from file1 and save first word to file third, then read from file2 and save first word from here to third file as second word and so on.. and also words from file1 have "+" at the start of the word and from file2 have "-". My problem is that code doesnt stop and is still working and printig to third file "."(lot of dots). thanks



#include<stdio.h>
#include<stdlib.h>

int main(){
FILE *first, *second, *third;
char ch[256],ch1[256],ch2;
int i=1,count;
char space = ' ';
char minus = '-';
char plus = '+';

first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");

if(first==NULL || second==NULL || third==NULL)
{
printf("error");
exit(1);
}

while (fscanf(first, "%255s", ch) == 1)
{
count++;
}

while (fscanf(second, "%255s", ch) == 1)
{
count++;
}

printf("%d",count);

for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}
fputc(space,third);
}


fclose(first);
fclose(second);
fclose(third);

return 0;
}









share|improve this question













I need to read from 2 different files and print words to third one, first i read from file1 and save first word to file third, then read from file2 and save first word from here to third file as second word and so on.. and also words from file1 have "+" at the start of the word and from file2 have "-". My problem is that code doesnt stop and is still working and printig to third file "."(lot of dots). thanks



#include<stdio.h>
#include<stdlib.h>

int main(){
FILE *first, *second, *third;
char ch[256],ch1[256],ch2;
int i=1,count;
char space = ' ';
char minus = '-';
char plus = '+';

first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");

if(first==NULL || second==NULL || third==NULL)
{
printf("error");
exit(1);
}

while (fscanf(first, "%255s", ch) == 1)
{
count++;
}

while (fscanf(second, "%255s", ch) == 1)
{
count++;
}

printf("%d",count);

for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}
fputc(space,third);
}


fclose(first);
fclose(second);
fclose(third);

return 0;
}






c file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 17:35









Rudolph

145




145












  • count is uninitialized
    – yano
    Nov 12 at 17:38












  • yes, but thats not the biggest problem. thanks
    – Rudolph
    Nov 12 at 17:39












  • Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
    – Antti Haapala
    Nov 12 at 17:41






  • 1




    using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
    – yano
    Nov 12 at 17:42






  • 1




    fgetc returns an int.
    – Antti Haapala
    Nov 12 at 17:43


















  • count is uninitialized
    – yano
    Nov 12 at 17:38












  • yes, but thats not the biggest problem. thanks
    – Rudolph
    Nov 12 at 17:39












  • Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
    – Antti Haapala
    Nov 12 at 17:41






  • 1




    using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
    – yano
    Nov 12 at 17:42






  • 1




    fgetc returns an int.
    – Antti Haapala
    Nov 12 at 17:43
















count is uninitialized
– yano
Nov 12 at 17:38






count is uninitialized
– yano
Nov 12 at 17:38














yes, but thats not the biggest problem. thanks
– Rudolph
Nov 12 at 17:39






yes, but thats not the biggest problem. thanks
– Rudolph
Nov 12 at 17:39














Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
– Antti Haapala
Nov 12 at 17:41




Instead of that useless printf("error") consider adopting perror("error") instead - it would provide a human-readable message telling what goes wrong!
– Antti Haapala
Nov 12 at 17:41




1




1




using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
– yano
Nov 12 at 17:42




using it without initializing it invokes undefined behavior. count++ is going to add to whatever garbage value happened to be there, it's impossible to know how many times your for loop will execute.
– yano
Nov 12 at 17:42




1




1




fgetc returns an int.
– Antti Haapala
Nov 12 at 17:43




fgetc returns an int.
– Antti Haapala
Nov 12 at 17:43












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Take a look at the two while loops here:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}


The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.



You want to check if ch is both not EOF and not a space:



while(ch2 != EOF && ch2 != ' ')


This would work if not for this:



char ch[256],ch1[256],ch2;


You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:



char ch[256],ch1[256]
int ch2;


Your while loops also don't have any braces around them. So what you actually have is:



            while(ch2 != EOF || ch2 != ' ') {
putc(ch2,third);
}
ch2=fgetc(first);


ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(second);
}
}


On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.



You need to call rewind(first) and rewind(second) before entering this loop.



Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.



Be sure to set it to 0:



int i=1,count=0;





share|improve this answer























  • thanks, solves many problems but my program still doesnt print characters to third file @dbush
    – Rudolph
    Nov 12 at 18:38












  • @Rudolph Did you add the rewind calls before the for loop?
    – dbush
    Nov 12 at 18:41










  • you are god @dbush
    – Rudolph
    Nov 12 at 18:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267324%2freading-from-two-different-files-and-printing-to-third-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Take a look at the two while loops here:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}


The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.



You want to check if ch is both not EOF and not a space:



while(ch2 != EOF && ch2 != ' ')


This would work if not for this:



char ch[256],ch1[256],ch2;


You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:



char ch[256],ch1[256]
int ch2;


Your while loops also don't have any braces around them. So what you actually have is:



            while(ch2 != EOF || ch2 != ' ') {
putc(ch2,third);
}
ch2=fgetc(first);


ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(second);
}
}


On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.



You need to call rewind(first) and rewind(second) before entering this loop.



Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.



Be sure to set it to 0:



int i=1,count=0;





share|improve this answer























  • thanks, solves many problems but my program still doesnt print characters to third file @dbush
    – Rudolph
    Nov 12 at 18:38












  • @Rudolph Did you add the rewind calls before the for loop?
    – dbush
    Nov 12 at 18:41










  • you are god @dbush
    – Rudolph
    Nov 12 at 18:45















up vote
2
down vote



accepted










Take a look at the two while loops here:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}


The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.



You want to check if ch is both not EOF and not a space:



while(ch2 != EOF && ch2 != ' ')


This would work if not for this:



char ch[256],ch1[256],ch2;


You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:



char ch[256],ch1[256]
int ch2;


Your while loops also don't have any braces around them. So what you actually have is:



            while(ch2 != EOF || ch2 != ' ') {
putc(ch2,third);
}
ch2=fgetc(first);


ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(second);
}
}


On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.



You need to call rewind(first) and rewind(second) before entering this loop.



Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.



Be sure to set it to 0:



int i=1,count=0;





share|improve this answer























  • thanks, solves many problems but my program still doesnt print characters to third file @dbush
    – Rudolph
    Nov 12 at 18:38












  • @Rudolph Did you add the rewind calls before the for loop?
    – dbush
    Nov 12 at 18:41










  • you are god @dbush
    – Rudolph
    Nov 12 at 18:45













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Take a look at the two while loops here:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}


The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.



You want to check if ch is both not EOF and not a space:



while(ch2 != EOF && ch2 != ' ')


This would work if not for this:



char ch[256],ch1[256],ch2;


You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:



char ch[256],ch1[256]
int ch2;


Your while loops also don't have any braces around them. So what you actually have is:



            while(ch2 != EOF || ch2 != ' ') {
putc(ch2,third);
}
ch2=fgetc(first);


ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(second);
}
}


On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.



You need to call rewind(first) and rewind(second) before entering this loop.



Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.



Be sure to set it to 0:



int i=1,count=0;





share|improve this answer














Take a look at the two while loops here:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(first);
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF || ch2 != ' ')
putc(ch2,third);
ch2=fgetc(second);
}


The first issue is that ch2 != EOF || ch2 != ' ' will always be true. If ch2 is EOF, the first condition is false but the second is true. If ch2 is a space, the second condition will be false and the first will be true. If it is neither, both are true.



You want to check if ch is both not EOF and not a space:



while(ch2 != EOF && ch2 != ' ')


This would work if not for this:



char ch[256],ch1[256],ch2;


You have ch2 defined as a char, while fgetc returns an int. The value EOF is out of range for a char, so you'll never see that value. Change it to int:



char ch[256],ch1[256]
int ch2;


Your while loops also don't have any braces around them. So what you actually have is:



            while(ch2 != EOF || ch2 != ' ') {
putc(ch2,third);
}
ch2=fgetc(first);


ch2 never changes inside of the loop, so you have an infinite loop. Put braces where they belong:



        if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ') {
putc(ch2,third);
ch2=fgetc(second);
}
}


On top of this, the file pointers for both first and second are at the end of each file before you enter the for loop, so there's nothing to read.



You need to call rewind(first) and rewind(second) before entering this loop.



Finally, count is not initialized. It could contain some randomly large number, so your loop could run many more times than you intend.



Be sure to set it to 0:



int i=1,count=0;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 18:19

























answered Nov 12 at 17:55









dbush

91.5k12100131




91.5k12100131












  • thanks, solves many problems but my program still doesnt print characters to third file @dbush
    – Rudolph
    Nov 12 at 18:38












  • @Rudolph Did you add the rewind calls before the for loop?
    – dbush
    Nov 12 at 18:41










  • you are god @dbush
    – Rudolph
    Nov 12 at 18:45


















  • thanks, solves many problems but my program still doesnt print characters to third file @dbush
    – Rudolph
    Nov 12 at 18:38












  • @Rudolph Did you add the rewind calls before the for loop?
    – dbush
    Nov 12 at 18:41










  • you are god @dbush
    – Rudolph
    Nov 12 at 18:45
















thanks, solves many problems but my program still doesnt print characters to third file @dbush
– Rudolph
Nov 12 at 18:38






thanks, solves many problems but my program still doesnt print characters to third file @dbush
– Rudolph
Nov 12 at 18:38














@Rudolph Did you add the rewind calls before the for loop?
– dbush
Nov 12 at 18:41




@Rudolph Did you add the rewind calls before the for loop?
– dbush
Nov 12 at 18:41












you are god @dbush
– Rudolph
Nov 12 at 18:45




you are god @dbush
– Rudolph
Nov 12 at 18:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267324%2freading-from-two-different-files-and-printing-to-third-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)