954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dat file txt file help????

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;
           
}
jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 
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????

jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

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

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

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??

jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Attachments employee.txt (0.27KB)
jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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??

Attachments 1.zip (0.28KB) employee.txt (0.27KB)
jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

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");          
      }
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Thanx friends you are the best byes..

jerryseinfeld
Junior Poster in Training
55 posts since Apr 2007
Reputation Points: 8
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You