I'm currently writing a chat program in C. It operates in two terminals, and uses one program. A sample input of session 1 would look like:
./a.out a.txt b.txt Phil
Where a.txt is the file to be used for input/reading, and b.txt is the file to be used for output/sending a message. Phil is the chat handle.
It follows that the input of session 2 looks like:
./a.out b.txt a.txt Jill
So that the input file is the output file of the first session, enabling the chat to work, and for the two terminals to talk to each other.
A sample run of this program would look something like:
Send: Are you there?
Received [Jill]: Yup!
Send: Okay see ya!
Received [Jill]: Bye!
^C
And vice versa in the other terminal. However, I'm having trouble getting my program to send the files and receive them automatically. My output looks correct, but I can only receive a message if I send one, which sort of defeats the purpose of the chat. My question is, where in my while loop am I going wrong, to where I have to send a message before I can read the one the other session has sent? Here is the code I have so far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[]) {
// Initialize file variables
FILE *in;
FILE *out;
// Initialize incoming, incoming check, and outgoing variables
char inc[300] = " ";
char incCheck[300] = " ";
char send[300];
char handle[300];
int size, counter;
counter=1;
// This checks if a user has already entered a message
// i.e. entered the chat
if (in = fopen(argv[1], "r")) {
fclose(in);
}
else {
// create an empty in.txt to bypass segfault if the file doesn't already exist
in = fopen(argv[1], "w");
fclose(in);
// To check if anything has been received yet.
if (strcmp(inc, incCheck) == 0) {
printf("Nothing has been received yet.\n");
}
}
// The while loop that reads and writes the files
while(1) {
// Read the incoming file and put it to a string
// It will read the incoming file over and over until a message is seen
in = fopen(argv[1], "r");
fgets(inc, size, in);
fclose(in);
// This counter is only for the first message, since it is not possible
// that one has already been received.
if (counter > 0) {
size=sizeof(send);
printf("Send: \t");
fgets(send, size, stdin);
out = fopen(argv[2], "w");
fprintf(out, "%s",send);
fclose(out);
counter=0;
}
// If the incoming file is different, print it out
if (strcmp(inc, incCheck) != 0) {
printf("Received [%s]: %s", argv[3], inc);
in = fopen(argv[1], "r");
fgets(inc, size, in);
strcpy(incCheck, inc);
fclose(in);
// And prompt to send another message.
size=sizeof(send);
printf("Send: \t");
fgets(send, size, stdin);
out = fopen(argv[2], "w");
fprintf(out, "%s",send);
fclose(out);
in = fopen(argv[1], "r");
fgets(inc, size, in);
fclose(in);
}
}
Setting one side of the chat as initiator solves the problem. Is that sufficient ? In code below this is solved by setting one of the handles as "s".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[]) {
// Initialize file variables
FILE *in;
FILE *out;
// Initialize incoming, incoming check, and outgoing variables
char inc[300] = " ";
char incCheck[300] = " ";
char send[300];
char oldsend[300];
char handle[300];
int size, counter;
counter=1;
// This checks if a user has already entered a message
// i.e. entered the chat
if (in = fopen(argv[1], "r")) {
fclose(in);
}
else {
// create an empty in.txt to bypass segfault if the file doesn't already exist
in = fopen(argv[1], "w");
fclose(in);
// To check if anything has been received yet.
if (strcmp(inc, incCheck) == 0) {
printf("Nothing has been received yet.\n");
}
}
in = fopen(argv[1], "r");
fgets(incCheck, size, in);
fclose(in);
in = fopen(argv[2], "r");
fgets(oldsend, size, in);
fclose(in);
// The while loop that reads and writes the files
while(1) {
// Read the incoming file and put it to a string
// It will read the incoming file over and over until a message is seen
// This counter is only for the first message, since it is not possible
// that one has already been received.
if ((counter > 0) && ( strcmp(argv[3],"s")==0)) {
size=sizeof(send);
do {
printf("Send: \t");
fgets(send, size, stdin);
}while (strcmp(send, oldsend)==0);
strcpy (oldsend,send);
out = fopen(argv[2], "w");
fprintf(out, "%s",send);
fclose(out);
counter=0;
}
in = fopen(argv[1], "r");
fgets(inc, size, in);
fclose(in);
}
// If the incoming file is different, print it out
if (strcmp(inc, incCheck) != 0) {
printf("Received [%s]: %s", argv[3], inc);
in = fopen(argv[1], "r");
fgets(inc, size, in);
strcpy(incCheck, inc);
fclose(in);
// And prompt to send another message.
size=sizeof(send);
do {
printf("Answer: \t");
fgets(send, size, stdin);
} while (strcmp(send, oldsend)==0);
strcpy (oldsend,send);
out = fopen(argv[2], "w");
fprintf(out, "%s",send);
fclose(out);
}
}
return 0;
}
Related
I am reading a growing input file, and do some work and write the info to the output file. I have some conditions to work on the growing file. But I am failing at exiting from the loop.
FILE *logfile;
int main(int argc, char *argv[])
{
char *filename;
char *logfilename;
FILE *infile;
char line_buf[255];
char *line;
sleep(3);
if (argc < 3) {
fprintf(stderr, "Usage: %s <filename> <logfile>\n",
argv[0]);
return -1;
}
filename = argv[1];
logfilename = argv[2];
infile = fopen(filename, "rb");
if (infile == NULL) {
fprintf(stderr, "Failed to open file\n");
return -1;
}
logfile = fopen(logfilename, "w");
if (logfile == NULL) {
fprintf(stderr, "Failed to open logfile - are permissions correct?\n");
return -1;
}
while(1){
line = fgets(line_buf, sizeof(line_buf), infile);
if (line == NULL){
if(feof(infile))
clearerr(infile);
failedReads++;
usleep(25000); //wait for the data from live file
continue;
}
else{
if(feof(infile))
break;
}
...........
//do some work
...........
}
fclose(infile);
fclose(logfile);
}
My output log file is getting the data only after the input file stops growing(means at the end of execution). I want my output logfile to get the data by time(means output file is not growing). I have a python script for create a growing file(If anyone really wants to work my issue).
#/usr/bin/python
import time
with open("input.txt") as f:
fileoutput = f.readlines()
with open("out.txt", "a+") as f1:
for line in fileoutput:
f1.write(line)
f1.flush()
time.sleep(0.01)
Code is waiting indefinitely for additional data. Code just waits longer and longer with usleep(25000*failedReads).
// Ineffective code
if (line == NULL){
if(feof(infile))
clearerr(infile);
failedReads++;
usleep(25000*failedReads); //wait for the data from live file
continue;
}
else{
if(line == NULL) // this can never be true!
if(feof(infile))
break;
}
"to get the data by time(means output file is not growing" implies there should be an upper bound of time that if the input file fails to provide more data, quit the loop.
Look for 2 successive read failures. If waiting after the first failure does not provide more data, time to leave.
// instead
bool failed_read_flag = false;
while(1){
line = fgets(line_buf, sizeof(line_buf), infile);
if (line == NULL){
if (failed_read_flag) {
break; // 2 successive read fails
}
if(!feof(infile)) {
break; // Rare input error just occurred.
}
clearerr(infile);
failed_read_flag = true;
usleep(25000 /* us */); //wait for the data from live file
continue;
}
failed_read_flag = false;
// Do some work
}
Strange to open a file with infile = fopen(filename, "rb"); and then use it with fgets().
It the file is a text file, use fopen(filename, "r"). If the file is binary, use fread().
I can't figure out why this isn't working.
#include <stdio.h>
int main(void) {
FILE *in, *out;
// char *FULLPATH = "C:\\Users\\Jay\\c\\workspace\\I-OFiles\\in.txt\\ ";
// char *mode = "r";
// in = fopen(FULLPATH, mode);
//
// if (in == NULL) {
// perror("Can't open in file for some reason\n");
// exit (1);
// }
out = fopen("C:\\Users\\Jay\\c\\workspace\\I-OFiles\\out.txt", "w");
if (out == NULL) {
perror("Can't open output file for some reason \n");
exit(1);
}
fprintf(out, "foo U");
fclose(in);
fclose(out);
return 0;
}
if I remove the // from the commented lines, the error compiler gives is
: Invalid argument
I don't understand why (I read all the other threads related, and nothing).
It does actually write the out.txt file OK, so it doesn't seem like a path misspelled problem.
Remove backslash after in.txt.
The input file name seems bogus:
"C:\\Users\\Jay\\c\\workspace\\I-OFiles\\in.txt\\ "
The filename is just a single space " " and in.txt is probably not a directory.
Change the code to:
const char *FULLPATH = "C:\\Users\\Jay\\c\\workspace\\I-OFiles\\in.txt";
Or preferably:
const char *FULLPATH = "C:/Users/Jay/c/workspace/I-OFiles/in.txt";
for better portability as forward slashes work in Windows as well as in Unix.
Furthermore, it is easy to provide more information as to why fopen() failed to open the files.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *in, *out;
in = fopen("C:/Users/Jay/c/workspace/I-OFiles/in.txt", "r");
if (in == NULL) {
perror("Cannot open input file");
exit(1);
}
out = fopen("C:/Users/Jay/c/workspace/I-OFiles/out.txt", "w");
if (out == NULL) {
fclose(in);
perror("Cannot open output file");
exit(1);
}
fprintf(out, "foo U");
fclose(in);
fclose(out);
return 0;
}
Change backslash to slash.
Maybe you don't have permissions or something like that.
out = fopen("C://Users//Jay//c//workspace//I-OFiles//out.txt", "w");
if (!out)
perror("fopen");
return 0;
I'm writing code that's supposed to verify that a .txt file is a certain format.
I wrote my code as I saw in a tutorial and in the website
and for some reason my program doesn't even print my file.
Can you tell me what I'm doing wrong?
The code will do something far more complex, but I'm still trying to work on my basics.
Here's my code so far:
int main(int argc, char *argv[]) {
/* argv[0] = name of my running file
* argv[1] = the first file that i receive
*/
define MAXBUFLEN 4096
char source[MAXBUFLEN + 1];
int badReturnValue = 1;
char *error = "Error! trying to open the file ";
if (argc != 2) {
printf("please supply a file \n");
return badReturnValue;
}
char *fileName = argv[1];
FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
if (fp != NULL) {
size_t newLen = fread(&source, sizeof(char), MAXBUFLEN, fp);
if (ferror(fp) != 0) {
printf("%s %s", error, fileName);
return badReturnValue;
}
int symbol;
while ((symbol = getc(fp)) != EOF) {
putchar(symbol);
}
printf("finish");
fclose(fp);
}
else {
printf("%s %s", error, fileName);
return badReturnValue;
}
}
I think you need a bit more explanations:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// there might be a macro BUFLEN defined in stdio
// which size is optimized for reading in chunks.
// Test if avaiable otherwise define it
#ifndef BUFLEN
# define BUFLEN 4096
#endif
int main(int argc, char *argv[])
{
char source[BUFLEN];
char *filename;
FILE *fp;
size_t fpread, written;
char c;
int ret_fclose;
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
// reset errno, just in case
errno = 0;
// work on copy
filename = malloc(strlen(argv[1]) + 1);
if (filename == NULL) {
fprintf(stderr, "Allocating %zu bytes failed\n", strlen(argv[1]) + 1);
exit(EXIT_FAILURE);
}
filename = strcpy(filename, argv[1]);
// try to open the file at 'filename'
fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Opening file \"%s\" filename failed\n", filename);
// errno might got set to something usable, check and print
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
// You have two options here. One is to read in chunks of MAXBUFLEN
while ((fpread = fread(&source, 1, BUFLEN, fp)) > 0) {
// Do something with the stuff we read into "source"
// we do nothing with it here, we just write to stdout
written = fwrite(&source, 1, fpread, stdout);
// you can use 'written' for error check when writing to an actual file
// but it is unlikely (but not impossible!) with stdout
// test if we wrote what we read
if ((fpread - written) != 0) {
fprintf(stderr, "We did not write what we read. Diff: %d\n",
(int) (fpread - written));
}
}
// fread() does not distinguish between EOF and error, we have to check by hand
if (feof(fp)) {
// we have read all, exit
puts("\n\n\tfinish\n");
// No, wait, we want to do it again in a different way, so: no exit
// exit(EXIT_SUCCESS);
} else {
// some error may have occured, check
if (ferror(fp)) {
fprintf(stderr, "Something bad happend while reading \"%s\"\n", filename);
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
}
// the other way is to read it byte by byte
// reset the filepointers/errors et al.
rewind(fp);
// rewind() should have reseted errno, but better be safe than sorry
errno = 0;
printf("\n\n\tread and print \"%s\" again\n\n\n\n", filename);
// read one byte and print it until end of file
while ((c = fgetc(fp)) != EOF) {
// just print. Gathering them into "source" is left as an exercise
fputc(c, stdout);
}
// clean up
errno = 0;
ret_fclose = fclose(fp);
// even fclose() might fail
if (ret_fclose == EOF) {
fprintf(stderr, "Something bad happend while closing \"%s\"\n", filename);
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
// The macros EXIT_FAILURE and EXIT_SUCCESS are set to the correct values for
// the OS to tell it if we had an eror or not.
// Using exit() is noot necessary here but there exits teh function atexit()
// that runs a given function (e.g: clean up, safe content etc.) when called
exit(EXIT_SUCCESS);
}
You read from the file twice but only print once.
If the file is to small the first reading will read all of the contents, and the second reading will not produce anything so you don't print anything.
I believe you have to reset the pointer after using fread.
Try fseek(fp, SEEK_SET, 0) to reset the pointer to the beginning of the file. Then print the file.
I need to open a file located on Desktop(Linux). If i write the location as a string inside the fopen() function it works, but if i pass it as a variable, it doesn't work. Here is my code :
fp = fopen(readPathToFile, "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
If i write it like this, it shows me the content of file :
fp = fopen("home/user/Desktop/test.txt", "r");
if (!fp){
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
The child process opens the file. Here is my full code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define READ 0
#define WRITE 1
int main ()
{
pid_t pid;
int mypipefd[2];
id_t child_pid;
char line[100];
char *pathToFile[100];
FILE *fp;
char buff[255];
/* create the pipe */
if (pipe(mypipefd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
child_pid = fork () ;
if (child_pid > 0) {
printf("Introduceti locatia catre fisier:");
fgets(pathToFile, 100, stdin);
close(mypipefd[READ]);
write(mypipefd[WRITE], &pathToFile, sizeof(pathToFile));
close(mypipefd[WRITE]);
printf("parent: write value : %s",pathToFile);
}
else if (child_pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
else{
char *readPathToFile[100];
close(mypipefd[WRITE]);
read(mypipefd[READ], &readPathToFile, sizeof(readPathToFile));
close(mypipefd[READ]);
printf("child: read value : %s",readPathToFile);
fp = fopen(readPathToFile, "r");
if (!fp)
{
printf("Failed to open text file\n");
exit(1);
}
else{
fscanf(fp,"%s",line);
printf("File read: %s",line);
}
}
return 0;
}
Your compiler did not warn you about the type mismatch in
char *pathToFile[100];
fgets(pathToFile, 100, stdin);
(array of 100 pointers-to-char versus array of 100 chars)? Did you turn warnings off?
Also note that fgets retains the newline. Your file name probably does not end with a newline. You should replace it with a NUL (zero) byte.
Typically you don't need a debugger to track these down. A little bit of printf debugging can do wonders. :-)
Okay, so this is the root of your problem:
char *pathToFile[100];
This declares pathToFile as a 100-element array of pointers to char, not a 100-element array of char. The first thing you need to do is change that declaration to
char pathToFile[100];
Secondly, fgets will save the trailing newline from your input to the target buffer if there's room, so you'll need to remove that newline from the input:
char *newline = strchr( pathToFile, '\n' );
if ( newline )
*newline = 0;
Here is my code I am trying to print out info like a Scanner in Java. How can I do so in C?
int main(){
char buffer[1000];
FILE *pFile;
pFile = fopen("randomnumbers.txt", "r");
if(!pFile){
printf("Error : Couldn't Read the File\n");
return 1;
}
}
{
puts(buffer);
}
printf("Success Reading from File\n");
if(fclose(pFile) != 0)
printf("Error : File Not Closed\n");
return 0;
}
I really hope this helps, this is how i could have done it.
int main(){
char buffer[1000];
//I think this means that you only expect there to be a maximum of 1,000
// characters per lines read.
FILE *pFile;
pFile = fopen("randomnumbers.txt", "r");
if(!pFile){
printf("Error : Couldn't Read the File\n");
return 1;
}
}
// You can also read from a file using fscanf
// You pass the file, data type to read and the buffer
// to store it in. You can check for valid data because
// fscanf() returns a number other then 1 if it isn't
// a string
while(fscanf(pFile, "%s", buffer) == 1){
// Puts outputs the string plus a newline.
// Returns a nonnegative integer if it was successful
// and EOF if there was an error
puts(buffer);
}
printf("Success Reading from File\n");
// Closes the text file
if(fclose(pFile) != 0)
printf("Error : File Not Closed\n");
return 0;
}
Replace with this:
int main(){
char buffer[1001]; // i have added one byte because all strings are ending with zero byte
FILE *pFile;
pFile = fopen("randomnumbers.txt", "r");
if(!pFile){
printf("Error : Couldn't Read the File\n");
return 1;
}
int bytesReaded = fread(
buffer, // output buffer
1, // char size is 1
1000, // length of buffer - 1
pFile // opened file
);
// Now you have readed the content of file
// And check if file was readed successfully
if(bytesReaded > 0){
buffer[bytesReaded] = '\0'; // all strings in C are ended with zero byte
// And now you can puts the content to console output
puts(buffer);
} else {
// File not readed successfully or is empty
}
printf("Success Reading from File\n");
if(fclose(pFile) != 0)
printf("Error : File Not Closed\n");
return 0;
}