i want to know how to read all the message inside the file including their spacing,indents if possible using File Handling... can that be done?

content of data1.txt:

First Name: (TAB)John Doe
Last name:(TAB) Manly
Height:(TAB) 5'7
Weight:(TAB)164 kls
Address:(TAB)Somewhere but here,
(TAB)but not to far City.

here is my program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

main()
{
FILE *fp1;
char x[100],y[100];
fp1=fopen("data1.txt","r");
while(!feof(fp1))
{ fscanf(fp1,"%s",&x);
printf("%s ",x);
}
fclose(fp1);
printf("\n\n");
system("pause");
}

can i use only one variable for all the content of the file??? i want to include all the grammars or spacing...can that be possible?

if my program is wrong from the start...please feel free to rewrite it...thank you all

Recommended Answers

All 2 Replies

use fgets() to read an entire line at one time. But since you are using c++ you really need to learn the <fstream> and <string> library fuctions.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
FILE *fp1;
char line[255];
fp1=fopen("data1.txt","r");
while(fgets(line, sizeof(line), fp1) != NULL)
{ 
     printf("%s ",line);
}
fclose(fp1);
printf("\n\n");
system("pause");
}

thank you very much ancient dragon...this is for my project...i'm a newbie btw...
i have another problem but i will post it some other time...

once again thank u ancient dragon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.