Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 10
- const char deteminator = ';';
- int ReadNextString(FILE *, char *, int);
- int main(int argc, char **argv) {
- char *vzorek = argv[1];
- char string[MAX];
- int read_return;
- int sloupec = 1;
- int radek = 1;
- FILE *file;
- if (argc == 2) {
- file = stdin;
- }
- else if (argc == 3) {
- if ((file = fopen(argv[2], "r")) == NULL) {
- fprintf(stderr, "Error opening file '%s'", argv[2]);
- exit(1);
- }
- }
- else {
- fprintf(stderr,"Arguments error");
- exit(1);
- }
- while(1) {
- memset(string, '\0', sizeof(char)*10);
- read_return = ReadNextString(file, string, MAX);
- if (strstr(string, vzorek) != NULL) {
- printf("%s sloupec: %d radek: %d\n", string, sloupec, radek);
- }
- if (read_return == 10) {
- radek++;
- sloupec = 1;
- }
- if (read_return == deteminator) {
- sloupec ++;
- }
- if (read_return == -1) {
- break;
- }
- if (read_return == -2) {
- fprintf(stderr, "Too long word, row: %d column: %d", radek, sloupec);
- exit(1);
- }
- }
- return 0;
- }
- int ReadNextString(FILE *file, char *string, int max) {
- int c;
- int counter = 0;
- while (1) {
- c = getc(file);
- if (feof(file)) {
- return -1;
- }
- if (c == 10) {
- return c;
- }
- if (c == deteminator) {
- return deteminator;
- }
- if (c != deteminator && counter >= max) {
- return -2;
- }
- else {
- string[counter] = c;
- counter++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement