0

For example, I am writing a C function that replace a substring s1 in string source with a new string s2. But I am having trouble with reading the input from stdin. I also want it to end until an EOF is met.

I search a lot about "read until EOF" and "read a string containing whitespace", but I didnt make it to work.

#include <stdio.h>
{
  char source[120], s1[20], s2[20];
  ...
  //what ever to input multiple cases of source, s1, and s2 until EOF is met
  replace(source, s1, s2);
  printf("%s\n",source); 

  return 0;
}
kexanie
  • 211
  • 3
  • 9
  • 1
    You probably wanted to ask _how to read a file_? – devnull Mar 07 '14 at 10:10
  • Actually I am working on a problem on an online judge system, and all the data are input from stdin ending with EOF. so... that's why I am asking.. – kexanie Mar 07 '14 at 10:14
  • Just to clarify this: There is **no** such characters as EOF. Due to this it cannot terminate anything nor could it be read. – alk Mar 07 '14 at 12:59

4 Answers4

1

You might like to do something like this:

char buffer[1234];

while (NULL != fgets(buffer, 1234, stdin))
{
  /* Do something with the 0-terniated content of buffer. */
}

if (ferror(stdin))
{
   /* An error occurred reading from stdin. */
}

For reference:


If you cannot define an upper limit for the number of characters until a new-line, the getline() function might be of interest, as it is capable to allocate as much memory as necessary to hold all characters until the next new-line.

alk
  • 69,737
  • 10
  • 105
  • 255
0

You can use feof function.

while(!feof(stdin)) {
     //read data with scanf, gets, fgets, etc...
}

To send an EOF press CTRL+D. View the manual for more information about feof function. (man feof on terminal)

gior91
  • 1,745
  • 1
  • 16
  • 19
  • thx, but any other alternatives? It's sad that the OJ restricts the use of feof function. – kexanie Mar 07 '14 at 10:23
  • the 'fgets' function does it automatically. It stops to read when it read a '\n' or a 'EOF'. char* buffer = malloc(DIM); char* read = fgets(buffer, DIM, stdin); – gior91 Mar 07 '14 at 10:29
  • The answer to "*“while( !feof( file ) )” is always wrong*" http://stackoverflow.com/a/5432517/694576 – alk Mar 07 '14 at 12:55
  • Thanks for your advice @alk...but is the second alternative, that I've proposed, correct according to you? – gior91 Mar 07 '14 at 13:06
  • Yes/no, `fgets()` **could** to the job. See my answer on details. – alk Mar 07 '14 at 13:22
0

Check this and give me your opinion :

#include <stdio.h>
int Replace_Function(String[] s1,String[]  s2){
String source[120], s1[20], s2[20];

//This is what u want???? 
while (getchar() != EOF)
 {
   S1=S2;
 }

  return 0;
 }
ASNAOUI Ayoub
  • 462
  • 2
  • 9
0

I think this piece of code may help you:

int c;
while ((c = getchar()) != EOF) {
   // do smt
}

We must declare c to be a big enough to hold any value that getchar return. So we don't use char since
c must be big enough to hold EOF (which is integer defined in <stdio.h>)

nvg58
  • 891
  • 1
  • 8
  • 17