Hi everyone again me again I have lots of questions...
I have a program which reads something into a txt file like notepad. But I need to read into a dat file. Here is my code

#include <stdlib.h>
#include <stdio.h>
main()
{
      int employeeNumber;
      char name[30];
      char engineeringDicipline[40];
      int yearOfBirth;
      FILE *cfPtr;
      
      if((cfPtr = fopen("employee.txt","r")) == NULL)
                printf("File could not be opened\n");
      
      else {
           
           printf("%-20s%-13s%-20s%10s\n", "EmployeeNumber", "Name","EngineeringDicipline", "YearOfBirth");
           printf("________________________________________________________________\n");
           fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);
           while (!feof(cfPtr)) 
           if(employeeNumber!=0){      
           printf("%-20d%10s  %-20s%7.2d\n",employeeNumber,name,engineeringDicipline,yearOfBirth);
           fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);}
           system("PAUSE");
           fclose(cfPtr);
           }
system("PAUSE");          
           return 0;
           
}

Recommended Answers

All 12 Replies

if((cfPtr = fopen("employee.txt","r")) == NULL)

this is okay I can read it but when I change this to

if((cfPtr = fopen("employee.dat","r")) == NULL)

it appears some stupid thnings.. like triangels or someting else. I cant read the file....
I mean what is the difference??? why cant I read from dat file????

There's no difference. manually check to make sure the file exists at the location from which you are trying to open it. Then check the file in notepad to make sure its contents are what you are expecting to read

I checked it and saved files differents format dat and txt. They include same things.
But I think only difference is dat file has at the end of lines a blank line.. Is that main problem?. If it is problem I have to make with this blank line and how can I do that??

What operating system and compile are you using? Where did you get those two files? If you are using MS-Windows and you got the files from someone running linux/unix, then your program may not know how to interpret the end-of-line characters correctly.

Also, if the files are small you can zip them up and attach them to your post so that we can have a look at them.

ok I tried it but system cant accept the dat file upload :(

you have to zip them with WinZip or PkZip which will given them a .zip extension.

I tried winrar but it didnt work now I try zip :)
in zip file my dat file and txt file my txt file...
so what is the problem??

The problem is not the data files, but that you are using feof() to test for end-of-line. That doesn't always work the way we would like it to. Here's a better solution

else {
           
           printf("%-20s%-13s%-20s%10s\n", "EmployeeNumber", "Name","EngineeringDicipline", "YearOfBirth");
           printf("________________________________________________________________\n");
           fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);

// changes start here
           while (fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth))
           {
                if(employeeNumber!=0){      
                    printf("%-20d%10s  %-20s%7.2d\n",employeeNumber,name,engineeringDicipline,yearOfBirth);
                }
           }
           fclose(cfPtr);
           system("PAUSE");          
      }

thank you so much last question..
what does rewind ???

rewind moves the file pointer back to the beginning of the file so that it can be read all over again.

thank you so much last question..
what does rewind ???

Are you refering to rewind()?.
rewind() is a file pointer (not the same that pointers) function. It reset
the file pointer back to position 0 at the start of the file. You deal with
file pointer when you work with random access files. Part of the functions in the tool box are fseek(), ftell() and rewind();

[Edit:] Sorry Ancient Dragon, I problably was still writing this when you posted.

Thanx friends you are the best byes..

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.