|
| Total Answers and Comments: 3 |
Last Update: February 20, 2008 Asked by: bismitapadhy |
|
| | |
|
Submitted by: stevenong2007 // // File: FindWord3.cc // Author: Steven Ong // // Purpose: /** * I used this program as a 'try out' on Netbeans 6.0 * with Cygwin (GCC)3.4.4 on Win2k * Aso to review the pointer operation in C * */ //
#include <stdio.h> #include <stdlib.h> #include <string.h>
// // //
int main(int argc, char** argv) { FILE *inFile; char c, buff[120], *t; int line = 1; if (argc != 3) { perror("Usage: findwordinfile [word] [fileName] n"); exit(1); } inFile = fopen(argv[2], "r"); printf("argv[1] = [%s]n", argv[1]); if(inFile == NULL){ perror("File cannot be opened!"); exit(1); } //reset the pointer to point to the begining of buff t = buff; do { c = fgetc(inFile); // This necessary for it to work on MS-Windows // In Windows, the newline goes in pair "rn" // Pain! Pain! Pain! if (c == 'r'){ continue; } if (c == 'n'){ if (strcmp(buff, argv[1])== 0){ printf("found [%s] at line %dn", buff, line); } line++; //Reset the buffer memset(buff, ' | |