2

I have been using file streams with c++ for years and can load character strings from a file like this:

char a[30],b[30],c[30];
ofstream fp;
fp.open("file.txt");
fp<<a<<b<<c;

Is there a way to read strings from a file in PHP in the same fashion without reading a whole line or file?

1
  • I'm not familiar with c++ - what exactly does this code do in c++?
    – TheWolf
    Commented Sep 29, 2013 at 21:57

1 Answer 1

1

I suppose you're looking for fscanf:

$handle = fopen("users.txt", "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
    list ($name, $profession, $countrycode) = $userinfo;
    //... do something with the values
}

One obvious difference is that you have to specify the format as the function argument (it's hard to be that reflective when working with dynamic types). The advantage, though, as you may be quite specific in which characters to take in, using formats like %[0-9]|%[a-zA-Z0-9@&;:,. /!?-] etc.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.