urgent help with file manipulation program


 
Thread Tools Search this Thread
Top Forums Programming urgent help with file manipulation program
# 1  
Old 01-11-2011
urgent help with file manipulation program

Hey, i am trying to write a program that takes multiple files as command line arguments, then outputs them to a single file. i also need the option to read them back out again. essentially what i am trying to create is an archiver, however, a very simple one. The program i have accomplished so far lets me copy one specified input file to a specified output file.
any help is needed urgent and greatly appreciated.

thanks

wez

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

main (int argc, char **argv)
{
    int bytes_read, bytes_written;
    struct stat inode;
    int input_fd, output_fd;
    char buffer[64];
    int eof = 0;
    int i;
       
    /* Check the command line arguments */
    if (argc != 3) 
    {
         printf("syntax is:         %s <fromfile> <tofile>\n", argv[0]);
        exit (1);
    }
  
    /* Check the input file exists and is a file */
    if ((stat(argv[1], &inode) == -1) || (!S_ISREG(inode.st_mode)))
    {
        printf("%s is not a file\n", argv[1]);
        exit(2);
    }
  
    /* Check that the output file doesnt exist */
    if (stat(argv[2], &inode) != -1)
    {
        printf("Warning: The file %s already exists. Not going to overwrite\n", argv[2]);
        exit(2);
    }

    /* Open the input file for reading */
    input_fd = open(argv[1], O_RDONLY, 0);
    if (input_fd == -1)
    {
        printf("%s cannot be opened\n", argv[1]);
        exit(3);
    }
   
    output_fd = open(argv[2], O_CREAT | O_WRONLY | O_EXCL , S_IRUSR|S_IWUSR);
    if (output_fd == -1)
    {
        printf("%s cannot be opened\n", argv[2]);
        exit(3);
    }

    /* Begin processing the input file here */
    while (!eof)
    {
        bytes_read = read(input_fd, buffer, sizeof(buffer));
    
        if (bytes_read == -1)
        {
            printf("%s cannot be read\n", argv[1]);
            exit(4);
        }
    
        if (bytes_read > 0)
        {
            bytes_written = write(output_fd, buffer, bytes_read);
            if (bytes_written == -1)
            {
                printf("There was an error writing to the file %s\n",argv[2]);
                exit(4);
            }

            if (bytes_written != bytes_read)
            {
                printf("Devistating failure! Bytes have either magically appeared and been written or dissapeard and been skipped.  Data is inconsistant!\n");
                exit(101);
            }
        } 
        else
        {
            eof = 1;
        }
    }
      close(input_fd);
    close(output_fd);

}

# 2  
Old 01-11-2011
Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.


Thank you.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. Programming

Wrapper for unix program - urgent help needed

Hello all , i need some help asap i have a program that keeps killing the machine when i did google searches and 2 days later i ran strace it seems the programm keeps making a system call to gettimeofday to i guess increment a counter ? gettimeofday({1347986584, 464904}, NULL) = 0... (6 Replies)
Discussion started by: NetworkLearning
6 Replies

4. Programming

An error in my c++ program... please help urgent.

The following is a program to convert an infix expression to postfix expression. //Convert an infix expression to postfix expression... #include<iostream> #include<string> #include<cstdlib> using namespace std; char ifx,pfx,stk; int top=-1,n; void push(char ch) { if(top!=n) ... (6 Replies)
Discussion started by: poonam.gaigole
6 Replies

5. Shell Programming and Scripting

Program Bash VERY URGENT

Hello I have to do a program in Bash, need help because it does not go out for me and go enough time with this!! Five directories(boards of directors) that more occupy, arranged according to size. To measure the size of every directory(board of directors) there must not be included the size... (1 Reply)
Discussion started by: danihj
1 Replies

6. UNIX and Linux Applications

Urgent---Program for getting Exception from a log

Hi Friends, I am new to Unix, Now I am Working with shell scripting in my company Description:-By executing that script I need to get the exception from the log file I need the program for getting the Exceptions from the logfile,,and that program should be generic...I mean if i want to... (5 Replies)
Discussion started by: Anji
5 Replies

7. UNIX for Advanced & Expert Users

Urgent Awk manipulation query help

Hi UNIX gurus :) , I have the following requirement and not being able to find a solution :( . i have got a file demo.lst movies 10 12 25 songs 11 8 15 DVD 15 12 10 Total 58 60 75 NOTE:-Total value not the sum of the columns but some predefined value. My... (5 Replies)
Discussion started by: rahul26
5 Replies

8. AIX

URGENT:Program is not dropping core on customer AIX Machine

hi We have a program which is running on cutomer end,and when its crashing its not dropping core, we asked them to check ulimit,they say that its unlimited. Even when they crash the program manually by using command kill -ABRT <pid> its not dropping the core,on our end when we use same... (1 Reply)
Discussion started by: khan_069
1 Replies

9. Shell Programming and Scripting

Very Urgent help required in Shell Program

How do I Ftp, and rename multiple files in one unix script. I have to send it with .tmp extension , then rename it to .txt after FTP is done . I need to do a Mass rename of more than 1 file in a shell script , Urgent help required. (1 Reply)
Discussion started by: Suppandi
1 Replies

10. UNIX for Dummies Questions & Answers

Urgent!! How to write a shell program to execute command to access internet?

hi, I am new ot unix. So, can i write a shell(c shell or korn shell) program to access internet? I mean if I run the program, it can access specified url and then copy the html to a file? Can anyone help me? And how can make the program runs every 1 hr? new comer (2 Replies)
Discussion started by: firebirdonfire
2 Replies
Login or Register to Ask a Question