2

I have little doubt about string reading in C.

string reading functions like gets, scanf, read, fscanf , fgets...

which C function can do a secure or safe string reading from any file?

Or

Which C function can be reliable to read a string in a file ?

sganesh
  • 1,731
  • 1
  • 13
  • 14
  • 1
    What do you mean by "secure" and "reliable"? –  Mar 11 '10 at 10:34
  • Yeah, you can't mention "C", "string" and those words in the same sentence. It's blasphemy! :) – Martin Wickman Mar 11 '10 at 10:37
  • For example if I have my password in one file I just want to transfer that data to server. In server, I need to read. That should not have any problems like buffer, and etc., – sganesh Mar 11 '10 at 10:39
  • 1
    This is not only about the functions you mentioned, but about security in C in general: http://stackoverflow.com/questions/2008173/writing-secure-c-and-secure-c-idioms – tanascius Mar 11 '10 at 10:39
  • In some scenario scanf will be better like that I want any inputs – sganesh Mar 11 '10 at 10:46

2 Answers2

2

An input function will be secure/safe(not allow buffer overflow) if it takes the length of the buffer as an arguments so that the function does not read more char than the buffer can hold.

Of the function you've listed fgets() and read() take the buffer length as argument and can be considered safe.

codaddict
  • 445,704
  • 82
  • 492
  • 529
0

Seems like you are simply asking how to read in a file. Google was helpful for me:

http://www.cprogramming.com/tutorial/cfileio.html

http://msdn.microsoft.com/en-us/library/aa365467%28VS.85%29.aspx

Read around or expand your question so we can answer you better.

Konrad
  • 39,751
  • 32
  • 78
  • 114
  • I know how to read a string from a file in a C. But I want to know the best one, – sganesh Mar 11 '10 at 11:12
  • Ok, that depends on how you want you intend to use the file and your platform. After writing helper classes for each, I would suggest the win32api (specifically CreateFile, WriteFile, ReadFile ... etc) if you are using Windows. – Konrad Mar 11 '10 at 17:30