Read from pipe large files C/Bash [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working on a web server in C. But for http request parsing and response I am using bash.
Communication between bash and server is going through fifo pipes.
I need to find a requested file like /index.html and send it through the pipe to server and then send it to client.
My code's looking like this:
bash:
cat $filename>/tmp/pipeToServer filename is something like index.html or image.jpg
And at server side I am reading it like:
pipe_r = open(/tmp/pipeToServer, O_RDONLY);
n = read(pipe_r, buffer2, sizeof(buffer2));
and buffer2 is char buffer[4096]
So my problem is that this is working only with small files like 1kb, but when the file is larger, I can't read the whole content at pipe because of a limit on the buffer size.

Increase the size of your buffer2 so that all the contents of the file are read. As of now maybe your buffer size is less than the contents of your file,so all values are not being read.

Related

Learning the syntax of files in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can't understand the syntax of writing in files in C.
I was requested in class to open a file named Numbers.txt and then print from 1 to 10 and each new line. I dont understand the syntax of it.
We use fopen,fwrite,fclose.
need help with understanding how to work with it.
You use a FILE to assign a file to something you can use. fopen opens a file depending on if the file exists or not.
FILE *pFile = fopen("Filename.foo","r");
This opens the file that is located in your project with the same filename. The 2nd parameter is the mode of the file — how you want to use it. Here is a table of modes. You must always check the return value to ensure it is not NULL, indicating that the program was unable to open the file as requested.
You always want to close the file when you're done editing it by using fclose.
fclose(pFile);
fwrite is one way of writing to a file; fprintf and family are another. Basically think about it like you opened a text editor and typed something in the file. You need to create a buffer (a place to store the numbers), then write the numbers into the file. Here is the reference for fwrite. I can't do all of your homework.

.txt Saving and naming the file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Thats the deal, I am trying to make and offline Email Manager, where you can write and store emails in different folders.
Written in .txt files that contain the basic information of an email (to:, from:, body :, etc). I am having issues on the following process.
-Each email has an ID that helps on searching and administration. I want to give the name of .txt files the ID related to the email that contains. So .... How can I create a .txt file that has the name of the ID and the ID is given by the user.
Thank you.
You have to create a buffer to build your filename.
#define BUFFERSIZE 256 /* max size of a path */
FILE * fp ;
char buffer[BUFFERSIZE] ;
snprintf(buffer, BUFFERSIZE, "/path/to/file/%s.txt", your_id_string);
fp = fopen(buffer, "w+") ;

Linux script in C get file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have own script in C for linux, it is working with string loaded from file. I compiled on server gcc -pthread -o pipeline pipeline.c. It is working. Now I am running this script any like this ./pipeline UPPERCASE LOWERCASE < my_file.txt. I can read all arguments but I dont know how to read file name behind < in this script. It is possible, or how is it working?
The contents of that file will show up on your process's standard input (stdin). You don't get the name of the file, as the same interface will be used in situations where there's no filename, such as when the output of another process is piped to yours (doSomething | pipeline UPPERCASE LOWERCASE), or when the user's terminal is used for input by default.

How do you upload a file from a server to a client in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to do this function:
put X Y : uploads the local file X to the server and names it Y at server side
I already wrote everything needed to set up the connection, I only want to know how to upload a file.
I only know the send() function which has 4 parameters, but it just seems to send an array of characters. How do you send an entire file, like an image for example?
Using HTTP, on Windows.
Just do this
Open the local file with open() for reading i.e.
int fd = open(localFileName, O_RDONLY);
if (fd == -1)
errorFileCantBeOpenedForReadingAbortThis();
Read bytes from the file and write them to the socket
int size;
unsigned char buffer[SOME_REASONABLE_SIZE];
size = read(fd, buffer, sizeof(buffer));
if (size == -1)
aReadErrorHandleIt();
if (write(sockfd, buffer, size) == -1)
aWriteErrorHandleIt();
do this in a loop until all the data was written.
In the server side, you need to write the data to the file that was probably specified through the put command or some custom command you might specify.
Of course you should first send the destination file name to the server along with something that indicates that a file write is about to start and open the file for writing.
It would be good if the server notifies the client that the writing might start and then the client could start writing until you send something to indicate that it was all sent, and the server could then close the file.

Stdin , stdout, stderr, how can I get my program to take input from a text file and output to a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In a programming competition , they mentioned this:
" How to answer to the problems?
In the problems you are up to solve, you have to read data from a file .IN, and write the results in a file OUT. "
I am used to the regular way of programming, writing the code in IDE , compiling , then executing it to see what's going on, and it worked, however my answers were refused.
Does anyone know anything about those file.IN and .OUT thing?
I mean, how can I get my program to take input from a textfile in which I write the dat aI want to give to the program, and make it send the the output to another textfile ?
Thanks
Include stdio.h in your header files.
In your main function, include the following lines on top.
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
In most online programming competitions, input is given through stdin and output through stdout. However, in this case, you have to read input from a file (.in) and write to a file (.out). freopen takes the stream(stdin, stdout) specified as the third argument to re-open the stream and instead use the file specified.
Edit: Sample code takes input from input.in and writes to output.out.
#include <stdio.h>
int main(){
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}

Resources