Hi, im newbie in C, now I want to make a program which can search something in file (now it will be two letter word) I want to command it from cmd by test text.txt ab

i have a problem with argv filed, everytime when i run this script it fall.... :(

dvojice - two letters from cmd
soubor - file

if can anybody help me, i will be very happy :)

#include <stdio.h> 

int main (int argc,char *argv[])
{
  char dvojice[2],c;
  FILE *soubor;
  int i,counter,umisteni,vysledek[1000];

  umisteni = 0;

dvojice[0] = argv[2][0];
dvojice[1] = argv[2][1];


  if (argv[1]) {
    soubor = fopen(argv[1],"r");
    if (soubor != NULL) {
      while((c=getc(soubor)) != EOF){
      umisteni ++;  
       if (c == dvojice[0]){
         c=getc(soubor);
         umisteni ++;
         if (c == dvojice[1]){
               counter ++;
               vysledek[counter]=umisteni;
               printf("lokace: %d\n",umisteni - 1);
               }
       }                        
      }   
      fclose(soubor);
      printf("Provedeno OK\n");
    } else {
      printf("Nepodarilo se otevrit soubor: %s\n",argv[1]);
    };
  } else {
    printf("Nebyl definovan nazev souboru!\n");
  };
  printf("Ukoncete stiskem klavesy Enter\n");
  getchar();
  return 0;
};

Recommended Answers

All 11 Replies

line 15. Wong check. You need to check argc to see if it is > 1. Then move lines 11 and 12 inside that if statement, between lines 15 and 16.

line 18: EOF is an integer, not a char. So you need to redefine variable c as int, otherwise if you leave it along that loop will never stop.

i have changed it to

#include <stdio.h> 

int main (int argc,char *argv[])
{
  char dvojice[2];
  FILE *soubor;
  int i,counter,umisteni,vysledek[1000],c;
  
  umisteni = 0;

  if (argc > 1) {
           
    dvojice[0] = argv[2][0];
    dvojice[1] = argv[2][1];
    
    soubor = fopen(argv[1],"r");
    if (soubor != NULL) {
      while((c=getc(soubor)) != EOF){
      umisteni ++;  
       if (c == dvojice[0]){
         c=getc(soubor);
         umisteni ++;
         if (c == dvojice[1]){
               counter ++;
               vysledek[counter]=umisteni;
               printf("lokace: %d\n",umisteni - 1);
               }
       }                        
      }   
      fclose(soubor);
      printf("Provedeno OK\n");
    } else {
      printf("Nepodarilo se otevrit soubor: %s\n",argv[1]);
    };
  } else {
    printf("Nebyl definovan nazev souboru!\n");
  };
  printf("Ukoncete stiskem klavesy Enter\n");
  getchar();
  return 0;
};

but still its not working... vista said that program "stop working, shutting down" (dont know en version of this)

problem must be somewhere here

dvojice[0] = argv[2][0];
    dvojice[1] = argv[2][1];

becouse when i delete this part, it will not fall...

but when i change this part to

dvojice[0] = 'g';
dvojice[1] = 'a';

its fall again...

I made up a file called "text.txt" with a bunch of random characters in it and typed in:

test text.txt ab

on the command line and it ran "fine". I put "fine" in quotes because it ran without crashing and giving me any of the errors it gave you, but I have no idea what it is supposed to do. I'm using Dev C++ on Microsoft Windows XP SP II. It compiled and ran without any errors with both the original "argv" lines and the ones you replaced them with (both from your last post, not your first). Can you elaborate on how/when you got the error, and what is in "text.txt"? Also, what is the program supposed to do?

huh... i see problem... :(

im using also dev c++, but on windows vista

in text.txt is random text, im trying this exe with text: "gargamel pelmel gar" so, im search "ga"

i want to learn t C, so i have bought book and in this is few examples to do... This is one of it. Search in file couple of letters, print in screen and into the file (i didnt do it, but i hope that i will use "vysledek" field) location of thes letters (first one)

problem is here

works:

printf("%s\n",argv[2]); //print "ga"

didnt work

printf("%s\n",argv[2][1]); //fall

You are printing a character, but telling "printf" that you are printing a string, I believe, in this line:

printf("%s\n",argv[2][1]); //fall

Change "%s" to "%c":

printf("%c\n",argv[2][1]); //fall

This time you are telling "printf" to print a character and giving it a character to print, so it works. Does this solve the problem?

yes, this work...

but i still dont know how to fix main problem - i dont want to print argv to screen, i only tested that.

What is the main problem? What is the program supposed to do? What is the program supposed to do that it does not do? What does the program do that it is not supposed to do? Also, what is the revised program (after revisions suggested here)?

what I want to do.... {i know, problem is also my english}
this is only part of final solution, but i have problem with run this

Final solution: {from my book, at end of the part about files}

open file and search the couple of letters, write to screen and into the another file their positon. two letters and file names insert from command line

cmd line: zadani13 text.txt ga
after this, it falling...

its only book example, but i want to know how i can do something like this. maybe will be easier, if can anybody wrote the main part and a will learn from you.... :(

many thanks for your help....

text.txt:

gargamel
gargamel

-> so i want to print (start position of "ga")

lokace: 1
lokace: 4
lokace: 9
lokace: 12

for print to file, (in the future) i hope that i can use vysledek[counter]

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
int main (int argc,char *argv[])
{
  char dvojice[2];
  FILE *soubor;
  int i,counter,umisteni,vysledek[1000],c;
  
  umisteni = 0;
 
  if (argc > 1) {
    
     dvojice[0] = argv[2][0];
     dvojice[1] = argv[2][1];

    soubor = fopen(argv[1],"r");
    if (soubor != NULL) {
      while((c=getc(soubor)) != EOF){
      umisteni ++;  
       if (c == dvojice[0]){
         c=getc(soubor);
         umisteni ++;
         if (c == dvojice[1]){
               counter ++;
               vysledek[counter]=umisteni;
               printf("lokace: %d\n",umisteni - 1);
               }
       }                        
      }   
      fclose(soubor);
      printf("Provedeno OK\n");
    } else {
      printf("Nepodarilo se otevrit soubor: %s\n",argv[1]);   
    };
  } else {
    printf("Nebyl definovan nazev souboru!\n");
  };
  printf("Ukoncete stiskem klavesy Enter\n");
  getchar();
  return 0;
};

I am going through your program now. So far I see one definite problem. You are using this line:

counter ++;

and this line:

vysledek[counter]=umisteni;

However, you have never initialized "counter" before using these lines. I believe the run-time error I ran into was because of this. You are overflowing the "vysledek" array because "counter" had a value above 1000, which is a segmentation fault in your program. Initializing counter to 0 like this:

counter = 0;

at the top of your program after "counter" is declared allows this program to run to completion. I'll post back with anything else I find, but I have found this so far.

yep, that works....

im sorry for all stupid questions, if i want to learn somethnig, i must be able to find errors like that :)

many thanks....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.