0% found this document useful (0 votes)
66 views

Dsa Lab Assignment 1

This document discusses C programming exercises involving command line arguments, file input/output, and data conversion. Exercise 1 involves passing command line arguments, printing the number of arguments and listing them. It also covers converting argument strings to integers. Exercise 2 covers locating and reading the stdio.h file, using fscanf and fprintf for file I/O, opening and closing files with fopen and fclose, writing a program to copy one file to another with error handling, and displaying the contents of a file given as a command line argument.

Uploaded by

priyanshu rane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Dsa Lab Assignment 1

This document discusses C programming exercises involving command line arguments, file input/output, and data conversion. Exercise 1 involves passing command line arguments, printing the number of arguments and listing them. It also covers converting argument strings to integers. Exercise 2 covers locating and reading the stdio.h file, using fscanf and fprintf for file I/O, opening and closing files with fopen and fclose, writing a program to copy one file to another with error handling, and displaying the contents of a file given as a command line argument.

Uploaded by

priyanshu rane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data structure and algorithm

lab assignment-1

exercise 1

Experiment – to implement command line arguments.


AIM- Passing command line argument using windows.
Problem 1a.- If you invoke the program test as follows, what will be the value of argc?
./cprogram hello world.
pseudo code-
- First we include a header file (stdio.h)
- We will write a main function with argc and argv.
Output terminal-

Result-
In the above example we are creating a simple command line utility. Here argc is for
“argument count” and argv for argument vector.
Argc is the integer parameter gives the number or arguments passed in command line and
argv is array of strings representing the individual arguments provided on command line.

Problem 1b- Modify the program test so that it prints “First C Program hello world” for the
above invocation. Generalize the program so that it prints the name of the program and all the
command line arguments in sequence.
pseudo code-
-First we will define a main function with parameters argc, and argv.
- print argc using integer format specifier and name of program using for loop from
p=0 to p<argc.
Then we will print argv[p] to get pth index position.

Result – argv[p] contains command line argument at p th index position.

Problem 1.c- Modify the program so that if a command line argument is an integer the
parameter string gets converted to an integer.

Pseudo code - Include stlidb.h function for atio() function to be executed


-define a main function with parameters argc, and argv.
- atoi() function converts a string to an integer.
Output –
Result - The atoi() function takes a string (which represents an integer) as an argument and
returns its value of type int. So basically the function is used to convert a string argument to
an integer.

Conclusion - Wrote a code to find the value of argc, implemented command line arguments
in a sequence, and also converted strings to integers using atoi function.

Exercise 2
Experiment – Files output /input.
Problem 2.a- Locate the file stdio.h in your computer, read and understand at least one pair
of I/O procedures. Note that the command find may be used to locate files by name. Refer to
the help pages for information on how find works.
Ans : for taking input we use “scanf(%formatspecifer”, & variable)” and for printing variable
and strings directly by we us “printf(%formatspecifer”, & variable)” . A pointer pointing to
the element which need to be searched in the data structure returned by Find(data). If it
doesn’t found then it will return a pointer pointing to the position just after the end of the data
structure.

Problem 2.b: C libraries support two procedures fscanf and fprintf for reading and writing to
a file.Refer to the man pages for information on how to use these procedures. They are
similar to scanf and printf but take an additional (first) argument that is a file pointer.

Ans: for reading a set of character from the file in file handling we use
fscanf(filepointer,”%formatspecifier” ,&data) for giving output when demanded by user.
For writing a file fprintf(filepointer, “%formatspecifier”,data) is used. It needs a file pointer
to read and write data from or in file.

Problem 2.C: libraries provide procedures fopen and fclose for initialization and finalization
of a file. Refer to the help pages for information on how to use these procedures.

Ans: for opening or initialising a file fopen(“nameOfFile.txt,”, “mode”) is used. There are
various ways for opening the file but it needs to be mentioned before opening the file
example of this are read only, write only & both. For closing or terminating the file that is
being pointed bt rh file pointer fclose(name of the file) is used.
Problem 2.d Write a C program that copies the contents of a file into a different file (given
two filenames as command line arguments). Error-proof your argument: i.e. detect and
print messages when errors occur - for instance, file is not present, unable to read/write,
etc. Also ensure that your program terminates gracefully when errors occur.

Pseudo code
- Include stdio.h and string .h
Write main function
Make a pointer to the first file.
IF the pointer is NULL exit.
Make a pointer to the second file.
IF the pointer is NULL exit.
WHILE end of file of first file is not reached
Copy characters from first file to second file
END WHILE
Close both the files.

Result – test for the file not existing.

Problem 2.e:
Pseudo code : ask a file as input using command line argument.
Make a pointer to the file to be displayed.
IF the pointer is NULL exit the code.
WHILE end of file of file is not reached
Display characters from file to display
END WHILE
Close the file.

You might also like