Dsa Lab Assignment 1
Dsa Lab Assignment 1
lab assignment-1
exercise 1
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.
Problem 1.c- Modify the program so that if a command line argument is an integer the
parameter string gets converted 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.
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.