| | |
dat file txt file help????
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 55
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#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; }
Teach me, Show me, Educate me :)
•
•
Join Date: Apr 2007
Posts: 55
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
if((cfPtr = fopen("employee.txt","r")) == NULL)
C Syntax (Toggle Plain Text)
if((cfPtr = fopen("employee.dat","r")) == NULL)
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 :)
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.
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.
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
c Syntax (Toggle Plain Text)
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"); }
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.
![]() |
Similar Threads
- Capturing data from txt file using JavaScript/HTML (JavaScript / DHTML / AJAX)
- dat,txt file and csv file???? (C++)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C Forum
- Previous Thread: Double executing same command?
- Next Thread: project of computerised telephone directory.
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming stack standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi







