C file never found - c

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.

Related

Sending .amr files to SIM900 over serial in c

I'm trying to send .amr files from my desktop to a SIM900 GSM module over UART.
I'm using teuniz's RS232 library.
I do the initialisation using AT commands and then read the file into a buffer and write it to the UART using the RS232_SendByte() library function byte-by-byte, but it doesn't seem to work.
I send the following AT commands:
AT+CFSINIT
AT+CFSWFILE=\"audio.amr\",0,6694,13000 # After which I get the CONNECT message from the SIM900 module
# Here's where I send the file
AT+CFSGFIS=\"audio.amr\"
Here's my code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "rs232.h"
char *readFile(char *filename, int *size) {
char *source = NULL;
FILE *fp = fopen(filename, "rb");
if (fp != NULL) {
/* Go to the end of the file. */
if (fseek(fp, 0L, SEEK_END) == 0) {
/* Get the size of the file. */
long bufsize = ftell(fp);
if (bufsize == -1) { return NULL; }
/* Allocate our buffer to that size. */
source = malloc(sizeof(char) * (bufsize + 1));
if(!source) return NULL;
/* Go back to the start of the file. */
if (fseek(fp, 0L, SEEK_SET) != 0) { return NULL; }
/* Read the entire file into memory. */
size_t newLen = fread(source, sizeof(char), bufsize, fp);
if ( ferror( fp ) != 0 ) {
fputs("Error reading file", stderr);
free(source);
return NULL;
} else {
source[newLen++] = 0; /* Just to be safe. */
}
*size = bufsize;
}
fclose(fp);
}
return source;
}
int main(int argc, char *argv[])
{
int ret = 0, cport_nr = 2, bdrate=38400;
char data[2000] = {0};
if(RS232_OpenComport(cport_nr, bdrate)) {
printf("Can not open comport\n");
ret = -1;
goto END;
}
int size;
unsigned char *filebuf = readFile("audio.amr", &size);
if (!filebuf) {
ret = -1;
goto END_1;
}
/* Initialization */
RS232_cputs(cport_nr, "AT");
RS232_cputs(cport_nr, "AT+CFSINIT");
sleep(1);
RS232_cputs(cport_nr, "AT+CFSWFILE=\"audio.amr\",0,6694,13000");
/* Wait for CONNECT */
sleep(2);
printf("Sending file of size: %d\n", size);
int i;
for (i = 0; i < size; ++i) {
putchar(filebuf[i]);
RS232_SendByte(cport_nr, filebuf[i]);
}
free(filebuf);
sleep(1);
/* Check if file transferred right */
RS232_cputs(cport_nr, "AT+CFSGFIS=\"audio.amr\"");
END_1:
RS232_CloseComport(cport_nr);
END:
return ret;
}
EDIT 1
Normally, the procedure to send a file to SIM900 using AT commands would be as documented here:
AT+CFSINIT # Initialize flash; Response is OK
AT+CFSWFILE=<filename>,<writeMode>,<fileSize>,<InputTime> # Write file with these parameter; Response is CONNECT; So this is when I start sending the file
Here's where I send the file. If it worked and the sent file size matched the <filesize> sent in the above command, SIM900 must respond with OK, which it doesn't. :(
AT+CFSGFIS=<filename> # Gives the file size on flash. This gives me an error since the file didn't upload correctly.
This leads me to beleive there's something wrong with my program. I'm reading the file in binary mode. And the size reported is exacty the same as I specify in the AT+CFSWFILE=<filename>,<writeMode>,<fileSize>,<InputTime> command.

I am trying to print a txt file and it doesn't work in C homework

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.

Unable to open file using c

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);

Trying to compute C to have an output input segment

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;
}

Read the content of a file to memory, result different from 32bit os and 64bit os

I wrote a function below to read the content of a file to memory.
It works well on my local machine(Ubuntu 32bit), but it produces wrong result on server(CentOS 64bit).
Wrong case:
With a 40 byte file, the content is below, on the 64bit os, it gave me wrong result.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The code:
char* file_get_contents(const char *filename) {
FILE *stream = NULL;
char *content = NULL;
size_t ret;
struct stat st;
if ((stream = fopen(filename,"r")) == NULL) {
fprintf(stderr, "Failed to open file %s\n", filename);
exit(1002);
}
if(stat(filename, &st) < 0) {
fprintf(stderr, "Failed to stat file %s\n", filename);
exit(1002);
}
content = malloc(st.st_size);
ret = fread(content, 1, st.st_size, stream);
if (ret != st.st_size) {
fprintf(stderr, "Failed to read file %s\n", filename);
exit(1002);
}
fclose(stream);
return content;
}
Your file_get_contents cannot be correctly used by its caller. It returns a char * but not its lenght, nor does it return a string (i.e. it isn't null terminated.).
As long as you're reading text, do e.g.
content = malloc(st.st_size + 1); // + 1 here for the nul terminator
ret = fread(content, 1, st.st_size, stream);
if (ret != st.st_size) {
fprintf(stderr, "Failed to read file %s\n", filename);
exit(1002);
}
content[st.st_size] = 0; //nul terminate

Resources