dat file txt file help????

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

dat file txt file help????

 
0
  #1
Apr 29th, 2007
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. }
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

Re: dat file txt file help????

 
0
  #2
Apr 29th, 2007
  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.
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: dat file txt file help????

 
0
  #3
Apr 29th, 2007
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.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

Re: dat file txt file help????

 
0
  #4
Apr 29th, 2007
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??
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: dat file txt file help????

 
0
  #5
Apr 29th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

Re: dat file txt file help????

 
0
  #6
Apr 29th, 2007
ok I tried it but system cant accept the dat file upload
Attached Files
File Type: txt employee.txt (274 Bytes, 2 views)
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: dat file txt file help????

 
0
  #7
Apr 29th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

Re: dat file txt file help????

 
0
  #8
Apr 29th, 2007
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, 2 views)
File Type: txt employee.txt (274 Bytes, 0 views)
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,571
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: dat file txt file help????

 
0
  #9
Apr 29th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 55
Reputation: jerryseinfeld is an unknown quantity at this point 
Solved Threads: 0
jerryseinfeld jerryseinfeld is offline Offline
Junior Poster in Training

Re: dat file txt file help????

 
0
  #10
Apr 29th, 2007
thank you so much last question..
what does rewind ???
Teach me, Show me, Educate me :)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC