944,162 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4911
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 29th, 2007
0

dat file txt file help????

Expand Post »
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
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. main()
  4. {
  5. int employeeNumber;
  6. char name[30];
  7. char engineeringDicipline[40];
  8. int yearOfBirth;
  9. FILE *cfPtr;
  10.  
  11. if((cfPtr = fopen("employee.txt","r")) == NULL)
  12. printf("File could not be opened\n");
  13.  
  14. else {
  15.  
  16. printf("%-20s%-13s%-20s%10s\n", "EmployeeNumber", "Name","EngineeringDicipline", "YearOfBirth");
  17. printf("________________________________________________________________\n");
  18. fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);
  19. while (!feof(cfPtr))
  20. if(employeeNumber!=0){
  21. printf("%-20d%10s %-20s%7.2d\n",employeeNumber,name,engineeringDicipline,yearOfBirth);
  22. fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);}
  23. system("PAUSE");
  24. fclose(cfPtr);
  25. }
  26. system("PAUSE");
  27. return 0;
  28.  
  29. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Apr 29th, 2007
0

Re: dat file txt file help????

  1. if((cfPtr = fopen("employee.txt","r")) == NULL)
this is okay I can read it but when I change this to
  1. 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????
Last edited by jerryseinfeld; Apr 29th, 2007 at 8:04 am.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Apr 29th, 2007
0

Re: dat file txt file help????

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
Last edited by Bench; Apr 29th, 2007 at 8:28 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Apr 29th, 2007
0

Re: dat file txt file help????

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??
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Apr 29th, 2007
0

Re: dat file txt file help????

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 29th, 2007
0

Re: dat file txt file help????

ok I tried it but system cant accept the dat file upload
Attached Files
File Type: txt employee.txt (274 Bytes, 29 views)
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Apr 29th, 2007
0

Re: dat file txt file help????

you have to zip them with WinZip or PkZip which will given them a .zip extension.
Last edited by Ancient Dragon; Apr 29th, 2007 at 9:28 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 29th, 2007
0

Re: dat file txt file help????

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??
Attached Files
File Type: zip 1.zip (291 Bytes, 32 views)
File Type: txt employee.txt (274 Bytes, 21 views)
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007
Apr 29th, 2007
0

Re: dat file txt file help????

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

  1. else {
  2.  
  3. printf("%-20s%-13s%-20s%10s\n", "EmployeeNumber", "Name","EngineeringDicipline", "YearOfBirth");
  4. printf("________________________________________________________________\n");
  5. fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth);
  6.  
  7. // changes start here
  8. while (fscanf(cfPtr, "%d%s%s%d", &employeeNumber,name,engineeringDicipline,&yearOfBirth))
  9. {
  10. if(employeeNumber!=0){
  11. printf("%-20d%10s %-20s%7.2d\n",employeeNumber,name,engineeringDicipline,yearOfBirth);
  12. }
  13. }
  14. fclose(cfPtr);
  15. system("PAUSE");
  16. }
Last edited by Ancient Dragon; Apr 29th, 2007 at 10:30 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 29th, 2007
0

Re: dat file txt file help????

thank you so much last question..
what does rewind ???
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
jerryseinfeld is offline Offline
55 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Double executing same command?
Next Thread in C Forum Timeline: Using the rand() function in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC