Using Linux System Calls to manipulate a text file

Can someone tell me what I'm doing wrong?

Page 1 of 1

8 Replies - 2709 Views - Last Post: 16 April 2010 - 12:03 AM Rate Topic: -----

#1 tfm217  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 05:50 PM

// Read and print from .txt file

#include <fcntl.h>     
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>


int main (int argc, char* argv[])
{
   char buf[13];
   size_t size = 13;
   open("C:/file.txt",O_RDWR);
   read(STDIN_FILENO, &buf, size);  //0?
   write(STDOUT_FILENO, buf, size);   //1?
   close(2);
   return 0;
}



-I'm unaware of how the buffer is supposed to be handled (This is my first time trying these things)
-I'm unsure of my use of the parameters for read write and close

This post has been edited by no2pencil: 14 April 2010 - 07:04 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Using Linux System Calls to manipulate a text file

#2 Guest_c.user*


Reputation:

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 06:59 PM

open("C:/file.txt",O_RDWR);

make an int variable and correct the path also

    int fd;

    if ((fd = open("./file.txt", O_RDWR)) < 0)
        return 1;

    /* close(2); */ /* stderr ? */

    close(fd);



you can use read(STDIN_FILENO, &buf, sizeof buf); also
but check the read returning value (it should be equal to sizeof buf - the third parameter)

This post has been edited by c.user: 14 April 2010 - 07:00 PM

Was This Post Helpful? 0

#3 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5307
  • View blog
  • Posts: 27,209
  • Joined: 10-May 07

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 07:04 PM

Maybe I'm missing something, but how is Linux ever going to have a C:\ drive?
Was This Post Helpful? 0
  • +
  • -

#4 tfm217  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 07:55 PM

View Postno2pencil, on 14 April 2010 - 06:04 PM, said:

Maybe I'm missing something, but how is Linux ever going to have a C:\ drive?


Heh...I was actually working on two similar programs at the same time and I had mistaken one for the other for a split second.

Thanks for your help so far
Was This Post Helpful? 0
  • +
  • -

#5 tfm217  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 10:22 PM

Hmm...Still nothing :-\ Any ideas?

#include <fcntl.h>     
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

int main (int argc, char* argv[])
{
   char buf[13];
   size_t size = sizeof(buf);
   int fd = open("./file.txt",O_RDWR);
   read(STDIN_FILENO, &buf, size);  //0?
   write(STDOUT_FILENO, buf, size);   //1?
   close(fd);
   return 0;
}

This post has been edited by tfm217: 14 April 2010 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 tfm217  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 10:41 PM

Changed some things around again. I feel like I have to be getting closer..

#include <fcntl.h>     
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>

int main (int argc, char* argv[])
{
   char buf[13];
   size_t size = strlen(buf);
   int fd; 
   fd = open("./file.txt",O_RDWR);
   read(fd, &buf, size);  //0?
   write(STDOUT_FILENO, buf, size);   //1?
   close(fd);
   return 0;
}


This post has been edited by tfm217: 14 April 2010 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5307
  • View blog
  • Posts: 27,209
  • Joined: 10-May 07

Re: Using Linux System Calls to manipulate a text file

Posted 14 April 2010 - 10:50 PM

I don't see where you initialized the file pointer fd. Nor do you verify that it contains a success after requesting to open the file.

What exactly are you trying to manipulate here?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5827
  • View blog
  • Posts: 12,682
  • Joined: 16-October 07

Re: Using Linux System Calls to manipulate a text file

Posted 15 April 2010 - 04:26 AM

Along the lines of what you have...

This works, and at least tells you where it's buggered.
#include <stdio.h>
#include <fcntl.h>     
#include <unistd.h>

#define BUFF_SIZE 13

int testFileIo(const char *filename) {
	int fd;
	
	if ((fd = open(filename, O_RDONLY))==0) {
		perror("Couldn't read file.");
		
	} else {
		char buf[BUFF_SIZE];
		int bytesRead = read(fd, &buf, BUFF_SIZE);
		close(fd); // nice to close asap
		
		if (bytesRead==0) {
			perror("Didn't read anything ");
			
		} else {
			printf("bytesRead=%d\n", bytesRead);
			write(STDOUT_FILENO, buf, bytesRead);
			return 0;
		}
	}

	return 1;
}

int main (int argc, char* argv[]) {
	return testFileIo("./file.txt");
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_c.user*


Reputation:

Re: Using Linux System Calls to manipulate a text file

Posted 16 April 2010 - 12:03 AM

for baavgai
        if (bytesRead==0) {
            perror("Didn't read anything ");
                        
        } else {



        if (bytesRead == -1)
            ...
        else if (bytesRead == 0)
            ...

?
Was This Post Helpful? 0

Page 1 of 1