I have the following code:
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
char* username, *password;
cout<<"Content-type: text/html"<<endl<<endl;
FILE *in = fopen("useri.txt","r");
while (fscanf(in,"%s %s",username, password) != EOF) {
cout<<username<<endl;
}
fclose(in);
}
The file looks like:
admin admin
For some reason, all the text in the specific file, is being read into the username var. The code as-is, does print out both the 2 values I have in my file (on separate lines), but that’s wrong, cause it should only print 1.
As soon as I try to print password, the program just exits, with no error (not even segmentation fault). Am I doing something wrong?
Note: I need this to stay C, not C++ (except for the cout part, because I’m lazy).
Memory is not allocated to variables.
use
malloc()to allocate memory dynamically.It is not mandatory to use only dynamic memory. you can use static memory also. i.e