-2

can anyone help me? I need to read a file called address.txt and work with each word in there (I have one word per line to make it simple).

I am forbidden to use these functions:

Malloc, freee, fopen, fclose, fscanf,... qsort, lsearch, bsearch a hsearch I think the teacher wants us to use getchar, argv, argc or something like that.

Let's say the name of the program is test.c ____ /test.c < address.txt

If I write this as an argument when starting the program, I should be able to work with the text file. Any tips how to extract the data stored in address.txt? I have tried several times without success :/ Only fopen worked for me.

EDIT: Everything I did was fine, Code::Blocks editing program caused the problems. I tried it in Linux terminal and it worked perfectly! :)

  • what have you tried so far? please add a [mcve] so we can see where you're going wrong. – Chris Turner Oct 23 '17 at 10:16
  • 1
    You may want to have a look at manuals of `read (2)`, `open (2)`, `write (2)`. The way you handle data inside all depends of the way it was stored. – Ra'Jiska Oct 23 '17 at 10:16
  • 1
    When you use input redirection, it will be `stdin` that is redirected. – Some programmer dude Oct 23 '17 at 10:17
  • 1
    Also the same question: [Read text file in C without fopen, fclose, fscanf](https://stackoverflow.com/q/46828573/2371524). –  Oct 23 '17 at 10:20

1 Answers1

1

Well yes, if your program is started like this:

$ ./test < address.txt

Then (assuming a compliant OS but this is fairly standard across major desktop systems) your program will start with a stdin stream connected to the contents of the file address.txt.

So you can use getchar() to read from it, and find the words.

Note that you won't be starting the C file, you must compile it first.

unwind
  • 391,730
  • 64
  • 469
  • 606