Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

How to have C pro read text file


  • Please log in to reply

#1
willmon2000

willmon2000

    Member

  • Member
  • PipPipPip
  • 215 posts
Hey so me and a fried are writing a pro which is basically a database. We want to store name and little of data on members who run the program (whit their consent they have to manually type it in). After they do the program creats a text file with all the info provided. Now the only problem is having the program reading the text file and display it when the user runs the program and lets it know he is a returning user. To say the truth im a newbi i just wrote hello world the other day so i dont know much :) . We will meet 2mrw and i want to know what to look up so that i will have something to contribute cuz he has been writing it :) .


this is just he shell it is not done yet he has the rest of the code i dont get to see it till 2mrw :) .


#include <stdio.h>

int main(void)
{
int MemberNum;
char *MemberName;

printf("How are you doing?\n");
printf("Are you a new member or returning member?\n");
printf("Please enter in one the following numbers:\n");
printf("--------------------------\n");
printf("1) New member\n");
printf("2) Returning member\n");
printf("3) Quit\n");
printf("--------------------------\n");
scanf("%i", &MemberNum);

while ( MemberNum != 3 )
{
switch ( MemberNum )
{
case 1:
printf("Enter your name: ");
scanf("%s", MemberName);
break;
case 2:
printf("Welcome back %s");
break;
case 3:
break;
default:
break;
}
}
}
  • 0

Advertisements


#2
Ypasos

Ypasos

    New Member

  • Member
  • Pip
  • 1 posts
I like your discussion.Thank you very much.
  • 0

#3
victor!

victor!

    New Member

  • Member
  • Pip
  • 4 posts
Try using something like this to read the file:
#include <stdio.h>
#include <stdlib.h>

int read_file( const char* pFileName )
{
	FILE* pFile = fopen( pFileName, "r" );
	if( pFile == NULL )
	{
		puts( "Error opening file.")
		return 1;
	}
	long fSize;
	fseek( pFile, 0, SEEK_END );
	fSize = ftell( pFile );
	rewind( pFile );
	char* buffer = ( char* ) malloc ( sizeof(char)*fSize );
	fread( buffer, 1, fSize, pFile );
	printf( "%s", buffer );
	fclose( pFile );
	free( buffer );
	return 0;
 
}

int main()
{
	read_file("name_of_file.txt");
	return 0;

}

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP