INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C
DETAILS
Question: Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character
becomes the last character of the file and so on)
Input: The program takes the file name whose content should be reversed.
Output: The program reverses the contents of the file.
Answer: #include #include int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); if (argc != 2) { printf("pass two argumentsn"); exit (-1); } /* Open the file */ fd = open(argv[1], O_RDWR); if (fd == -1) { printf("error in file openingn"); exit (-1); }#ifdef PRINT printf("File Open Sucessn");#endif /* Find the file size */ fsize = lseek(fd, 0, SEEK_END);#ifdef PRINT printf("File Size = %dn", fsize);#endif /* Reverse the file */ for (i=0; i
|
Question:
Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character
becomes the last character of the file and so on)
Input: The program takes the file name whose content should be reversed.
Output: The program reverses the contents of the file.
Answer:
#include #include int main(int argc, char *argv[]){ int fd, i, fsize; char *first=(char *) malloc(1); char *last=(char *) malloc(1); if (argc != 2) { printf("pass two argumentsn"); exit (-1); } /* Open the file */ fd = open(argv[1], O_RDWR); if (fd == -1) { printf("error in file openingn"); exit (-1); }#ifdef PRINT printf("File Open Sucessn");#endif /* Find the file size */ fsize = lseek(fd, 0, SEEK_END);#ifdef PRINT printf("File Size = %dn", fsize);#endif /* Reverse the file */ for (i=0; i Source: CoolInterview.com
#include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<string.h>
int main() { int fd=0,i=0,len=0; char buf[100]; fd=open("file.txt",O_RDONLY);
read(fd,buf,sizeof(buf)); len=strlen(buf);
for(i=len;i>=0;--i) printf("%c",buf[i]); printf("
");
} Source: CoolInterview.com
Answered by: anoj | Date: 9/8/2007 12:33:06 PM
| Contact anoj
The above program makes an assumption that the length of the file is less than 100. If the Source: CoolInterview.com
Answered by: abhimanipal | Date: 3/20/2010
| Contact abhimanipal
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|