Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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