Reputation: 1
I am wondering how to use the system calls read() and write() in C. I am trying to read in the contents of a pre existing, file within a directory, into a buffer (array) so I can step through the array and determine what type of file was read. I have looked at quite a few different posts on the matter and have not been able to figure out where I am going wrong. I am trying to print out my buffer array at the bottom to make sure it holds the correct contents of a file before stepping though it to determine the file type, but the buffer holds nothing. Any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
char *currentDir = NULL;
DIR *myDir = NULL;
struct dirent *myFile = NULL;
struct stat myStat;
const void *buf [1024];
int count;
int currentFile;
if (strcmp(argv[1], "ls") == 0 && argc < 3)
{
currentDir = getenv("PWD");
myDir = opendir(currentDir);
while ((myFile = readdir(myDir)) != NULL)
{
if (myFile->d_name[0] != '.')
{
puts(myFile->d_name);
//printf("%s\n", myFile->d_name);
}
}
closedir(myDir);
}
if (strcmp(argv[1], "ls") == 0 && strcmp(argv[2], "-t") == 0)
{
currentDir = getenv("PWD");
myDir = opendir(currentDir);
while ((myFile = readdir(myDir)) != NULL)
{
if (myFile->d_name[0] != '.')
{
printf("%s\n", myFile->d_name);
stat (myFile->d_name, &myStat);
printf("Last Accessed:\t%s\n", ctime(&myStat.st_atime));
printf("Last Modified:\t%s\n", ctime(&myStat.st_mtime));
printf("Last Changed:\t%s\n", ctime(&myStat.st_ctime));
}
}
closedir(myDir);
}
if (strcmp(argv[1], "ls") == 0 && strcmp(argv[2], "-f") == 0)
{
currentDir = getenv("PWD");
myDir = opendir(currentDir);
while ((myFile = readdir(myDir)) != NULL)
{
//while (count = read(0, buf, 100) > 0)
//{
//}
//write (1, buf, 100);
//printf ("Buffer Holds:\n %s\n", buf);
if (myFile->d_name[0] != '.')
{
while (count = read(myFile->d_name, buf, 100) > 0)
write (1, buf, count);
printf ("Buffer Holds:\n %s\n", buf);
}
}
}
return 0;
}
Upvotes: 0
Views: 2369
Reputation: 1
I was able to figure out what was going wrong, I did have to change the buf array to an array of chars, but I had some misconceptions on how read was working. I though that read()
was reading bytes from the file and storing it into a temp array, so I thought I needed to use write()
to write the information from a temp array into the array that I specified. In actuality, read()
read the specified file and stored its contents directly into my char buf [1024]
array, so the call to write()
was actually overwriting all the information read()
had read from the specified file, and stored into the char buf [1024]
array.
Thank you all for the reply's, I have only posted on here 1 other time, so I am still trying to figure out how to explain the issues I am encountering with less ambiguity.
Upvotes: 0
Reputation: 2185
You need some more parens here:
while (count = read(myFile->d_name, buf, 100) > 0)
try:
while ((count = read(myFile->d_name, buf, 100)) > 0)
Also, recommend using sizeof:
while ((count = read(myFile->d_name, buf, sizeof(buf))) > 0)
But you've declared buf
as an array of pointers:
const void *buf [1024];
which doesn't seem likely to be what you actually want. Are there really pointer values stored in the file? I think you probably meant for buf
to be an array of chars:
char buf[1024];
Upvotes: 1