@MatteoItalia brings up a good point in the comments (about piecing C together this way). This question was originally posted stating that it was part of an assignment - as Matteo indicates, I would not suggest this process for any sort of 'real' code.
The reason you will see people get frustrated when people don't show what they've done is because most people here love learning and figuring things out. I don't belong in the same room as most of the people on this site, but I think we all share that :)
I know absolutely nothing about C, but here is what I would do to figure out your issue. As mentioned by others, the internet and some dot-connecting can help you solve basically anything, so make sure to use it :)
First things first - how to open files in C? Let's Google it. Skip to the part where it talks about opening and see the code is something like:
FILE *fp;
fp=fopen("test.txt", "r");
I know I need to put this into a file that will be compiled with the C compiler, but I have no idea how to run it (using Linux). Google again. Looks like you use gcc
, -o
looks like it means output
and will return a file named whatever I type, and the final argument is the C file itself.
gcc -o learning learning.c
Now I get errors when I compile/run, and I'm going to assume its because I need to do a little more than just type the code above. Google a little more. Looks like I need to add something called 'headers'. These look like Python libraries, maybe they provide additional functionality (from my limited Python experience, I know that C seems to be considered extremely optimized, so perhaps part of that optimization comes from not including certain features as 'built-ins'. Again, not sure, just guessing still).
#include <stdio.h>
#include <conio.h>
void main() {
FILE *fp;
fp = fopen("passwrd.txt", "a");
fprintf(fp, "%s\n ", "Hello World, Where there is will, there is a way.");
fclose(fp) ;
}
For me this causes an error with conio.h
. I'll look into it later, but delete for now and try again. Now it works, adding that line to the file. However I don't want this - I want to read the file. I'll try changing a
to r
(since that looks like the Python a
, and I'm assuming it means append
; r
will read). I'll also cut out the other arguments to fprint
and see what happens. No dice, apparently I need to learn how to read a file. Google and I see this:
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
I drop that in my code in place of fprintf(fp);
, get another error about undeclared variables and libraries, add them in and get the following, which works:
#include <stdio.h>
#include <stdlib.h>
void main() {
char ch, file_name[25];
FILE *fp;
fp = fopen("passwd.txt", "r");
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
fclose(fp);
}
Now I need to print the first and the last values when a string is split. No clue how to do that, let's check the internet (and SO). Alright, let's drop in a new library and see if we can split stuff. I'll save the copy/paste, but I run into more and more errors. I don't know much about programming, but everything I come across seems to indicate errors with allocations, variable declarations, etc. I finally get something working (after finding a new way to read lines from a file - the other way looks like it was characters). Combining this with what I learned earlier about fprint
(looks like it behaves like string formatting in Python), I get this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )
{
char *token;
char *search = ":";
char line[256];
static const char filename[] = "passwd.txt";
FILE *file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ];
while ( fgets ( line, sizeof line, file ) != NULL )
{
token = strtok(line, search);
printf("First: %s\n", token);
}
fclose ( file );
}
else
{
perror ( filename );
}
return 0;
}
Now I need the end - I find this and see that I can make a bunch of calls to strtok
and chunk down the string. Do some more research on how to loop and get this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )
{
char *token;
char *search = ":";
char line[256];
static const char filename[] = "passwd.txt";
FILE *file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
token = strtok(line, search);
int counter;
for (counter = 0; counter < 5; counter ++)
strtok(0, search);
char *last = strtok(0, search);
printf("User %s has shell %s", token, last);
}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
return 0;
}
Is it perfect? No way. Do I know what it does? Kind of, but I need to research more to better understand it. All-in-all, find a way to break it into small chunks - the answers may be in different places, but they'll almost always be there :)