I have created one function which filters data from a data file and prints it to another file using redirection operator of unix
below is the function
void getdetailbyparam(char *name,char *type,int maxprice,int minprice)
{
printf("NAME:%s\nTYPE:%s\nMAX PRICE:%d MIN PRICE:%d\n",name,type,maxprice,minprice);
char getdetailbyparamfilelocation[1024];
snprintf(getdetailbyparamfilelocation, sizeof(getdetailbyparamfilelocation), "\"%s/getdetailbyparam.txt\"",cwd);
char command[1024];
snprintf(command, sizeof(command),"awk -F['|'] '{if (($3 ~ /.*%s.*/) && ($5==\"%s\") && ($7 >= %d) && ($7 <= %d)) print $7,$3,$1}' OFS=\" | \" %s | sort -n > %s",name,type,minprice,maxprice,databasefilelocation,getdetailbyparamfilelocation);
printf("Command is : %s\n",command);
system(command);
printf("File %s created\n",getdetailbyparamfilelocation);
}
Just need to give paths in "getdetailbyparamfilelocation" and "databasefilelocation".
Now When I call this function the file is created but when I try to open this file after calling the function it is giving me error of "No such file or directory"
Please see the following code
void funct(int sock)
{
char getdetailbyparamfilelocation[1024];
snprintf(getdetailbyparamfilelocation, sizeof(getdetailbyparamfilelocation), "\"%s/getdetailbyparam.txt\"",cwd);
size_t len = 0;
ssize_t read;
FILE *fp;
printf("Start sending data from the file at %s\n",getdetailbyparamfilelocation);
char *line = NULL;
fp = fopen(getdetailbyparamfilelocation, "r");
printf("Reading file \n");
if(fp == NULL)
{
perror("Error");
exit(1);
}
while((read = getline(&line, &len, fp)) != -1)
{
char sendline[1024];
int retu;
snprintf(sendline, sizeof(sendline), line);
printf("SERVER SENDING :%s\n",sendline);
retu = send(sock, line, strlen(line), 0);
}
}
Basically I am coding a client server sytem in which server reads the filtered results and send them to client to displays on client's terminal
Please help and also let me know if any further information is required
You need the double quotes around the file name only if you pass it to the shell. You don't need them when passing the file name to fopen.
Instead of
snprintf(getdetailbyparamfilelocation,
sizeof(getdetailbyparamfilelocation),
"\"%s/getdetailbyparam.txt\"",cwd);
Use
snprintf(getdetailbyparamfilelocation,
sizeof(getdetailbyparamfilelocation),
"%s/getdetailbyparam.txt",cwd);
Related
I have recently begun C and want to open a file, read the content of the file and then write to new file while adding in connector words inside the {} of the text.
However, I am not so sure whether I am getting the set-up for open/read correctly.
For example:
#define MAX_READ 20
int main(int argc, char *argv[]){
char buffer[MAX_READ + 1];
ssize_t numRead;
char connector[2] = ["and", "or"];
if(argc > 1){
int file = open(argv[1], O_RDWR);
if(file == -1){
perror("open");
exit(1);
}
numRead = read(file, buffer, MAX_READ)
if(numRead == -1){
perror("read");
exit(1);
}
}
}
Here is my sample text:
I am running {} swimming together with my friend {} brother
Expected output:
Open the file contents
Read the files content into a variable; match {} and replace each by the connector.
Write the result to a NEW file.
Output:
I am running and swimming together with my friend or brother
I am trying to read following file from C code.
file: /sys/bus/iio/devices/iio\:device0/in_voltage7_raw
but file pointer I am getting is -1.
Using cat command it is able to read the file.
But I am trying to read the same from my code as follows:
nos_int32 nos_adc_read_port (ADC_PORT_DB *p_port, nos_int32 *data)
{
char file_name[VALUE_MAX];
int value;
char buffer[BUFFER_LENGTH];
char intBuffer[INT_BUFFER_LENGTH];
int fd;
sprintf(file_name, "/sys/bus/iio/devices/iio\\:device0/in_voltage7_raw");
fd = open(file_name, O_RDONLY);
if (fd == -1) {
return(-1);
}
if (read(fd, buffer, BUFFER_LENGTH) == -1) {
return(-1);
}
close(fd);
memcpy(intBuffer, buffer, BUFFER_LENGTH);
intBuffer[INT_BUFFER_LENGTH-1] = '\0';
value = atoi(intBuffer);
*data = value;
return(0);
}
After the line:
fd = open(file_name, O_RDONLY);
value of fd is -1. How can it be solved?
Most command line shells use some characters for special actions and if you're trying to use them as their actual character, you need to prefix them with a backslash to escape them. In this case, your shell needs you to escape the colon when accessing that filename.
In C you don't have this issue so you can put in your code the filename as it truly is, such as:
"/sys/bus/iio/devices/iio:device0/in_voltage7_raw"
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;
}
I'm making a socket server that will for example take using input like "localhost:8080/WWW/index.html" and it will return the index.html file. I'm able to parse the request so that when someone inputs that I will have a buffer of "/WWW/index.html" and from the location of my program if I did vim /WWW/index.html I would open if but for some reason my code always says that the file is not found. Here is the following code...
char* parseRequest(char* request) {
//assume file paths are no more than 256 bytes + 1 for null.
char *buffer = malloc(sizeof(char)*257);
memset(buffer, 0, 257);
if(fnmatch("GET * HTTP/1.*", request, 0)) return 0;
sscanf(request, "GET %s HTTP/1.", buffer);
return buffer;
}
int fileExists(const char *fname){
FILE *file;
if(file = fopen(fname, "r"))
{
fclose(file);
return 1;
}
return 0;
}
recv(sock,buffer,255,0);
//reading inputted directory
char *dir = parseRequest(buffer);
if(fileExists(dir) == 1){
send(sock, "File found", 200, 0);
}else{
send(sock, "404: File not found", 200, 0);
}
Where dir is equal to "/WWW/index.html"
#define MAXBUF 256
#define DOCUMENTS_ROOT "/home/longbear/hw4"
char path[MAXBUF] = DOCUMENTS_ROOT;
strcat(path, dir);
.
.
.
Now you should be able to open the file at path.
I have created a Client/Server application in C by using the SSL library. the issue i am facing is each time i send a file there are some bytes missing in the start of file.
let suppose the text file which i am sending contains
123456789
and when the client receive the file it would contains
56789
Server-Code
void sendFile(SSL* ssl)
{
char response[2048] = {0};
int read = 0;
FILE* fd;
fd = fopen("snt.txt","rb");
if (fd == NULL)
{
printf("file loading failed\n");
return;
}
while ((read=fread(response,sizeof(char),1024,fd)) > 0)
{
SSL_write(ssl,response,read);
printf("read :%d\n",read);
//puts(response);
//printf("***Data Sent***\n");
memset(response,0,1024);
}
printf("***Data Sent***\n");
fclose(fd);
}
Client Code
FILE *ft;
char filebuf[2048];
int read = 0;
int error_check=0;
ft = fopen("rcv.txt","ab");
if (ft == NULL)
{
printf("Can not open file to write\n");
return -1;
}
memset(filebuf,0,2048);
int cnk=1;
while ((error_check=BIO_read(bio,&read,sizeof(int)))>0)
{
//printf("%d read\n",read);
if (error_check==0)
break;
if (read==0)
break;
BIO_read(bio,filebuf,read);
printf("%d Chunk Recieved\n",cnk++);
//puts(filebuf);
fwrite(filebuf,sizeof(char),strlen(filebuf),ft);
memset(filebuf,0,2048);
}
printf("***File Recieved***");
fclose(ft);
the other issue is client side is not terminated, control doesn't get away from the while-loop, kindly guide me how can i tackle these issues
Assuming size(int) is 4, I'd say the 1st 4 bytes are read by this line:
while ((error_check=BIO_read(bio,&read,sizeof(int)))>0)
That leaves the rest of the data sent to this line:
BIO_read(bio,filebuf,read);
The latter reads it into filebuf which then is written to the file rcv.txt.