Two files into one column in C Program
I am trying to figure it out why am I still getting error message when I did my gcc
command on terminal and I have also both included for code and compiler as well. Can anyone have any idea why or can help me for me? I am really new to C Program this semester. This is a main function that takes command line argument that opens two files and combines the two files one line at a time into one output. The first file are lines of text, but remove any trailing white spaces from the end of each line (newlines, tabs and spaces), and the second files are the list of numbers. So there should be two columns separated by a character. I have them for example so you can visual for more clarification:
Example for to output:
./p2 test/p2-testa test/p2-testb
Test A 11
Test B 51
Test C 91
Test D 26
Test E 17
Test F 76
/* 3 point */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose((fp) && (fp2));
return (0);
}
ERROR COMPILE MESSAGE (BTW: For lecture, I used labcheck
by instructor):
p2:
p2.c: In function ‘main’:
p2.c:52:19: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
fclose((fp) && (fp2));
~~~~~^~~~~~~~
In file included from p2.c:2:
/usr/include/stdio.h:199:26: note: expected ‘FILE *’ {aka ‘struct _IO_FILE *’} but argument is of type ‘int’
extern int fclose (FILE *__stream);
~~~~~~^~~~~~~~
-3.0 output of program (p2) is not correct for input '/u1/h7/CS151/.check/text/list.1 /u1/h7/CS151/.check/nums/tiny.1':
------ Yours: ------
---- Reference: ----
Line A 6
Line B 41
Line C 52
Line D 3
Line E 36
Line F 61
--------------------
I didn't really understand the warning and expected message in C program.
c file fclose
add a comment |
I am trying to figure it out why am I still getting error message when I did my gcc
command on terminal and I have also both included for code and compiler as well. Can anyone have any idea why or can help me for me? I am really new to C Program this semester. This is a main function that takes command line argument that opens two files and combines the two files one line at a time into one output. The first file are lines of text, but remove any trailing white spaces from the end of each line (newlines, tabs and spaces), and the second files are the list of numbers. So there should be two columns separated by a character. I have them for example so you can visual for more clarification:
Example for to output:
./p2 test/p2-testa test/p2-testb
Test A 11
Test B 51
Test C 91
Test D 26
Test E 17
Test F 76
/* 3 point */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose((fp) && (fp2));
return (0);
}
ERROR COMPILE MESSAGE (BTW: For lecture, I used labcheck
by instructor):
p2:
p2.c: In function ‘main’:
p2.c:52:19: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
fclose((fp) && (fp2));
~~~~~^~~~~~~~
In file included from p2.c:2:
/usr/include/stdio.h:199:26: note: expected ‘FILE *’ {aka ‘struct _IO_FILE *’} but argument is of type ‘int’
extern int fclose (FILE *__stream);
~~~~~~^~~~~~~~
-3.0 output of program (p2) is not correct for input '/u1/h7/CS151/.check/text/list.1 /u1/h7/CS151/.check/nums/tiny.1':
------ Yours: ------
---- Reference: ----
Line A 6
Line B 41
Line C 52
Line D 3
Line E 36
Line F 61
--------------------
I didn't really understand the warning and expected message in C program.
c file fclose
1
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49
add a comment |
I am trying to figure it out why am I still getting error message when I did my gcc
command on terminal and I have also both included for code and compiler as well. Can anyone have any idea why or can help me for me? I am really new to C Program this semester. This is a main function that takes command line argument that opens two files and combines the two files one line at a time into one output. The first file are lines of text, but remove any trailing white spaces from the end of each line (newlines, tabs and spaces), and the second files are the list of numbers. So there should be two columns separated by a character. I have them for example so you can visual for more clarification:
Example for to output:
./p2 test/p2-testa test/p2-testb
Test A 11
Test B 51
Test C 91
Test D 26
Test E 17
Test F 76
/* 3 point */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose((fp) && (fp2));
return (0);
}
ERROR COMPILE MESSAGE (BTW: For lecture, I used labcheck
by instructor):
p2:
p2.c: In function ‘main’:
p2.c:52:19: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
fclose((fp) && (fp2));
~~~~~^~~~~~~~
In file included from p2.c:2:
/usr/include/stdio.h:199:26: note: expected ‘FILE *’ {aka ‘struct _IO_FILE *’} but argument is of type ‘int’
extern int fclose (FILE *__stream);
~~~~~~^~~~~~~~
-3.0 output of program (p2) is not correct for input '/u1/h7/CS151/.check/text/list.1 /u1/h7/CS151/.check/nums/tiny.1':
------ Yours: ------
---- Reference: ----
Line A 6
Line B 41
Line C 52
Line D 3
Line E 36
Line F 61
--------------------
I didn't really understand the warning and expected message in C program.
c file fclose
I am trying to figure it out why am I still getting error message when I did my gcc
command on terminal and I have also both included for code and compiler as well. Can anyone have any idea why or can help me for me? I am really new to C Program this semester. This is a main function that takes command line argument that opens two files and combines the two files one line at a time into one output. The first file are lines of text, but remove any trailing white spaces from the end of each line (newlines, tabs and spaces), and the second files are the list of numbers. So there should be two columns separated by a character. I have them for example so you can visual for more clarification:
Example for to output:
./p2 test/p2-testa test/p2-testb
Test A 11
Test B 51
Test C 91
Test D 26
Test E 17
Test F 76
/* 3 point */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose((fp) && (fp2));
return (0);
}
ERROR COMPILE MESSAGE (BTW: For lecture, I used labcheck
by instructor):
p2:
p2.c: In function ‘main’:
p2.c:52:19: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
fclose((fp) && (fp2));
~~~~~^~~~~~~~
In file included from p2.c:2:
/usr/include/stdio.h:199:26: note: expected ‘FILE *’ {aka ‘struct _IO_FILE *’} but argument is of type ‘int’
extern int fclose (FILE *__stream);
~~~~~~^~~~~~~~
-3.0 output of program (p2) is not correct for input '/u1/h7/CS151/.check/text/list.1 /u1/h7/CS151/.check/nums/tiny.1':
------ Yours: ------
---- Reference: ----
Line A 6
Line B 41
Line C 52
Line D 3
Line E 36
Line F 61
--------------------
I didn't really understand the warning and expected message in C program.
c file fclose
c file fclose
edited Nov 15 '18 at 9:56
H.S.
5,4161319
5,4161319
asked Nov 15 '18 at 7:42
Mike ODellMike ODell
226
226
1
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49
add a comment |
1
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49
1
1
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49
add a comment |
2 Answers
2
active
oldest
votes
Expression (fp) && (fp2)
passed to fclose
combines two pointers by operator &&
, which expects integral operands and interprets them as beeing either ==0
or !=0
. The result is an integral value, which again is either ==0
or !=0
, but it has nothing to do any more with the pointers fclose
expects.
So fclose((fp) && (fp2))
should be
fclose(fp);
fclose(fp2);
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls tofclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().
– paulsm4
Nov 15 '18 at 7:55
add a comment |
your program should look exactly like this
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose(fp);
fclose(fp2);
return (0);
}
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314547%2ftwo-files-into-one-column-in-c-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Expression (fp) && (fp2)
passed to fclose
combines two pointers by operator &&
, which expects integral operands and interprets them as beeing either ==0
or !=0
. The result is an integral value, which again is either ==0
or !=0
, but it has nothing to do any more with the pointers fclose
expects.
So fclose((fp) && (fp2))
should be
fclose(fp);
fclose(fp2);
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls tofclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().
– paulsm4
Nov 15 '18 at 7:55
add a comment |
Expression (fp) && (fp2)
passed to fclose
combines two pointers by operator &&
, which expects integral operands and interprets them as beeing either ==0
or !=0
. The result is an integral value, which again is either ==0
or !=0
, but it has nothing to do any more with the pointers fclose
expects.
So fclose((fp) && (fp2))
should be
fclose(fp);
fclose(fp2);
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls tofclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().
– paulsm4
Nov 15 '18 at 7:55
add a comment |
Expression (fp) && (fp2)
passed to fclose
combines two pointers by operator &&
, which expects integral operands and interprets them as beeing either ==0
or !=0
. The result is an integral value, which again is either ==0
or !=0
, but it has nothing to do any more with the pointers fclose
expects.
So fclose((fp) && (fp2))
should be
fclose(fp);
fclose(fp2);
Expression (fp) && (fp2)
passed to fclose
combines two pointers by operator &&
, which expects integral operands and interprets them as beeing either ==0
or !=0
. The result is an integral value, which again is either ==0
or !=0
, but it has nothing to do any more with the pointers fclose
expects.
So fclose((fp) && (fp2))
should be
fclose(fp);
fclose(fp2);
answered Nov 15 '18 at 7:45
Stephan LechnerStephan Lechner
29.4k42143
29.4k42143
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls tofclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().
– paulsm4
Nov 15 '18 at 7:55
add a comment |
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls tofclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().
– paulsm4
Nov 15 '18 at 7:55
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls to
fclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().– paulsm4
Nov 15 '18 at 7:55
you beat me to it - cool! 1) As you said, the OP needs to make two separate calls to
fclose)
- he can't just close two file pointers at the same time. 2) The "expected" message is probably coming from labcheck. The problem there is probably incorrect output: the OP ALSO needs to add some code to "trim" the raw output from fgets().– paulsm4
Nov 15 '18 at 7:55
add a comment |
your program should look exactly like this
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose(fp);
fclose(fp2);
return (0);
}
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
add a comment |
your program should look exactly like this
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose(fp);
fclose(fp2);
return (0);
}
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
add a comment |
your program should look exactly like this
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose(fp);
fclose(fp2);
return (0);
}
your program should look exactly like this
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
const int MAXLEN = 4096;
const int MAXLINES = 10;
int main(int argc, char *argv) {
char buffer[MAXLEN];
char buffer2[MAXLEN];
FILE *fp = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
if (!(fp && fp2)) {
perror ("Not Found");
exit (EXIT_FAILURE);
}
int n = 0;
while((n < MAXLINES) && (fgets (buffer, sizeof (buffer), fp)) && (fgets(buffer2, sizeof (buffer2), fp2))) {
printf("%st%s", buffer, buffer2);
n++;
}
fclose(fp);
fclose(fp2);
return (0);
}
edited Nov 15 '18 at 7:58
answered Nov 15 '18 at 7:53
Canton RobinsonCanton Robinson
62
62
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
add a comment |
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
Q: Have you actually tried this, with test files? Do you get the expected results? Do the rows line up?
– paulsm4
Nov 15 '18 at 9:09
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314547%2ftwo-files-into-one-column-in-c-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Really not cool to create 2 questions on the platform for the same problem! in less than an hour interval!!
– PhoenixBlue
Nov 15 '18 at 7:49