Parsing a file line by line

Started by
19 comments, last by Machlana 19 years, 3 months ago
This has always confused me.... Does anybody know how to parse a file line by line for instance i want to read this file print(hello) sayhi do_nothing just_read exit how would i parse that file line by line in c i just dont get it
Advertisement
Checkout fscanf

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt_fscanf.2c_.fwscanf.asp
It depends on how long ang how efficient you want the parsing algorithm to be.
If it's only going to be a few lines long, you could just use some sort of loop to parse each individual character of a line until you find the newline character...

[EDIT]
You would have to load the file in a buffer first...
If you simply want to read a file line-by-line until there are no more lines to read (until you are at the end of the file), then check out the following code. I have put comments in to explain what is going on. Make sure you include <stdio.h>...

// Step 1: Open the file and store a handle to the file in pFile//FILE* pFile = fopen("somefile.txt", "r");	// <<< The "r" specifies that you want read only accessif ( !pFile ){	// Failed to open	return;}	// Step 2: Read the file until we are at the end of the file//while ( !feof(pFile) ){	char szStringBuf[256];	fgets( szStringBuf, 256, pFile );	// szStringBuf now stores the current line that you are parsing}// Step 3: Make sure to close the file once you have finished//fclose(pFile);
A line isnt always 256 characters etc...

Usualy the line terminates with /n (for newline) and /0 for eof. You can use the iostream library to grab just one line and then use scanf() to pull things from that line.

cin.getline(buffer, '\n', maxlen); //I believe these are the parameters, however they may be in a different order.. www.msdn.microsoft.com :)

If you don't have unix man pages, but would like the benefit, you can google "man fprintf" and usually the first link will be a manpage that someone put online. Very useful. There are also sites where you can search through manpages specifically, but I lost the link.

:Found one.
If i do this


// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("somefile.txt", "r"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
// Failed to open
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// ADDED
if (szStringBuf == CMD_PRINT)
{
printf("CMD_PRINT successfully called!");
}
else if( szStringBuf == CMD_WAIT )
{
printf("CMD_WAIT successfully called!");
}
else
{
printf("COMMAND NOT VALID!");
}
// END ADDED CODE

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);



would that work?
if so how would i get params?

im trying to implement a command based language trying to keep it simple using stdio.h
That's not likely to work...
Firstly, I'd have to see what the definitions are for CMD_PRINT and CMD_WAIT.
The code you worte will probably fail due to the way you are handling strings of text that are read from the file you opened...
Depending on the definition of CMD_PRINT and CMD_WAIT, and the data that is stored in szStringBuf you may or you may not be able to compare using the == operator...

According to your first post, you are trying to read a text file that has several commands in each line...
Example:

Wait
Print

I would use fgets to get a line from the file handle you opened...then I would use strcmp to compare the string that was read with a list of commands inside your app....according to the result, and using a loop, I'd do it recursively until the EOF and all commands have been processed...
Look for information on MSDN...if you still have trobule, I could write an easy example to illustrate the method...

If you are looking for performance, there are algorithms way more effective...
as of now im not really looking for performance i looked on msdn and no help :S
if you could, would you write a example just to open a file and parse it line by line and compare the strings like print (forget about wait) cuz after that i gotta implement params please if u could :)
Well i got it to read the string
however the last thing is params

this is my code (thanks to every1)

#include <stdio.h>

#define CMD_PRINT "print"
#define CMD_WAIT "wait"

int main()
{


// Step 1: Open the file and store a handle to the file in pFile
//
FILE* pFile = fopen("s", "rb"); // <<< The "r" specifies that you want read only access
if ( !pFile )
{
printf("ERROR");
return;
}


// Step 2: Read the file until we are at the end of the file
//
while ( !feof(pFile) )
{
char szStringBuf[256];
fgets( szStringBuf, 256, pFile );

// ADDED
if ( stricmp (szStringBuf, CMD_PRINT) == 0 )
{
printf("CMD_PRINT successfully called!");
}
else
{
printf("COMMAND NOT VALID!");
}
// END ADDED CODE

// szStringBuf now stores the current line that you are parsing
}


// Step 3: Make sure to close the file once you have finished
//
fclose(pFile);



}


so how would i get print to be able to use params if you could include that in the example :)

This topic is closed to new replies.

Advertisement