changeset 0:60596de1cb2c

upload read.c readline.c
author nobuyasu
date Mon, 31 Jan 2011 03:50:41 +0900
parents
children 80a0c0eb67e0
files Makefile read.c readline.c test.txt
diffstat 4 files changed, 107 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Mon Jan 31 03:50:41 2011 +0900
@@ -0,0 +1,15 @@
+# Makefile
+cc = gcc
+#cc = /Users/nobuyasuoshiro/hg/CbC/build-CbC/INSTALL_DIR/bin/gcc
+#CFLAGS = -g -O2 -Wall
+CFLAGS = -g -Wall
+all: readline read
+readline.o: readline.c
+read.o: read.c
+
+readline: readline.o
+	$(cc) $(CFLAGS) -o $@ $^
+read: read.o
+	$(cc) $(CFLAGS) -o $@ $^
+clean:
+	rm -f *.o *.s readline read
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/read.c	Mon Jan 31 03:50:41 2011 +0900
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <stdlib.h> //exit()
+#include <string.h> //strlen()
+
+#define MAX_LEN 256
+
+int grep(char* string, char* pattern);
+
+
+int main(int argc, char **argv)
+{
+  FILE *fp;
+  char string[MAX_LEN];
+  char *filename;
+  char *pattern;
+
+  
+  if(argc > 2) {
+    pattern = argv[1];
+    filename = argv[2];
+  } else{
+    pattern = "abc";
+    filename = "./test.txt";
+    printf("pattern = %s \nfilename = %s \n",pattern,filename);
+  }
+  if ((fp = fopen(filename, "r")) == NULL) {
+    fprintf(stderr, "not found %s\n",filename);
+    exit(2);
+  }
+
+  while( (fgets(string, MAX_LEN - 1, fp)) != NULL){
+    string[strlen(string) - 1] = '\0';
+    grep(string, pattern);
+  }
+
+  fclose(fp);
+
+  return 0;
+}
+
+int grep(char* string, char* pattern)
+{
+  char *str_;
+  char *pattern_;
+  do{
+    str_ = string;
+    pattern_ = pattern;
+  }while(*pattern_++ == *str_++);
+  if(*pattern_ == '\0')
+    return 1;
+  while(*++string != '\0'){}
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readline.c	Mon Jan 31 03:50:41 2011 +0900
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h> //exit()
+#include <string.h> //strlen()
+
+#define MAX_LEN 256
+
+int main(int argc, char **argv)
+{
+  FILE *fp;
+  char string[MAX_LEN];
+  int n = 1;
+  const char *filename;
+
+  if(argc > 1) {
+    filename = argv[1];
+  } else{
+    filename = "./test.txt";
+  }
+  if ((fp = fopen(filename, "r")) == NULL) {
+    fprintf(stderr, "can't open %s\n",filename);
+    exit(2);
+  }
+  
+  while((fgets(string, MAX_LEN - 1, fp)) != NULL) {
+    string[strlen(string) - 1] = '\0';
+    printf("%d:",n++);
+    puts(string);
+  }
+  fclose(fp);
+
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test.txt	Mon Jan 31 03:50:41 2011 +0900
@@ -0,0 +1,7 @@
+iuisdufi
+
+
+ababcifuiasdufiasd
+
+
+asdfiuasdoifj