| | |
help me to read my data file:((
![]() |
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi all,
I need ur help..
Actually I have to read the data file which containing character and digits. I need to converts some characters into digit.
The data file is like:
A11111
B22222
B33333
B4444
A55555B66666
B77777
So far my programming is like this:
The display result is like:
data[0]=A11111 atol test=77777
data[1]=B22222 atol test=77777
data[2]=B33333 atol test=77777
....
data[6]=B77777 atol test=77777
The display result is not satisfied for me. Actually I need to display the result something like this:
data[0]=A11111 atol test=11111
data[1]=B22222 atol test=22222
data[2]=B33333 atol test=...
data[3]=B44440 ---(need to add zero)
data[4]=A55555---(B66666 need to be entered to the next line)
data[5]=B66666....
data[6]=B77777 ....
...
Please help me...
I need ur help..
Actually I have to read the data file which containing character and digits. I need to converts some characters into digit.
The data file is like:
A11111
B22222
B33333
B4444
A55555B66666
B77777
So far my programming is like this:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> #define EOL '\n' #define N 100 void OpenFile(void); void ReadFile(void); void ConvData(void); char data[N]; FILE *dataptr; unsigned int i,ii; char *data2=malloc(strlen(data)); long k; main() { clrscr(); OpenFile(); if (dataptr!=0) { ReadFile(); printf("read\n"); fclose(dataptr); } { ConvData(); } return 0; } void OpenFile(void) { dataptr=fopen("data.dat","r"); if (dataptr==0){ printf("ERROR - Cannot open the file\n"); } return; } void ReadFile(void) { printf("DATA FILE:\n"); while (!feof(dataptr)) { printf("%-40s",data); fscanf(dataptr,"%s",data); } } void ConvData(void) { dataptr=fopen("data.dat","r"); for(i=0;i<=6;i++){ fscanf(fptdrill,"%s",data); printf("\ndata[%i]=%s",i,data); } if (data2!=NULL){ memset(data2, 0, sizeof data); { for (ii = 0, i = 0; i < strlen(data); i++) if (isdigit(data[i])) data2[ii++] = data[i]; } for (i=0;i<=6;i++) { k = atol(data2); { fscanf(fptdrill,"%s",data); printf("\t atol test:%ld\n",k); } } } } }
data[0]=A11111 atol test=77777
data[1]=B22222 atol test=77777
data[2]=B33333 atol test=77777
....
data[6]=B77777 atol test=77777
The display result is not satisfied for me. Actually I need to display the result something like this:
data[0]=A11111 atol test=11111
data[1]=B22222 atol test=22222
data[2]=B33333 atol test=...
data[3]=B44440 ---(need to add zero)
data[4]=A55555---(B66666 need to be entered to the next line)
data[5]=B66666....
data[6]=B77777 ....
...
Please help me...
Last edited by WaltP; Mar 6th, 2007 at 1:09 am. Reason: Added CODE tags -- did you miss the letters on the background of the input box of your post?
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
Sorry guys, wrong post for my previous program..this is the right program:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> #define EOL '\n' #define N 100 void OpenFile(void); void ReadFile(void); void ConvData(void); char data[N]; FILE *dataptr; unsigned int i,ii; char *data2=malloc(strlen(data)); long k; main() { clrscr(); OpenFile(); if (dataptr!=0) { ReadFile(); printf("read\n"); fclose(dataptr); } { ConvData(); } return 0; } void OpenFile(void) { dataptr=fopen("data.dat","r"); if (dataptr==0){ printf("ERROR - Cannot open the file\n"); } return; } void ReadFile(void) { printf("DATA FILE:\n"); while (!feof(dataptr)) { printf("%-40s",data); fscanf(dataptr,"%s",data); } } void ConvData(void) { dataptr=fopen("data.dat","r"); for(i=0;i<=6;i++){ fscanf(dataptr,"%s",data); printf("\ndata[%i]=%s",i,data); } if (data2!=NULL){ memset(data2, 0, sizeof data); { for (ii = 0, i = 0; i < strlen(data); i++) if (isdigit(data[i])) data2[ii++] = data[i]; } for (i=0;i<=6;i++) { k = atol(data2); { fscanf(dataptr,"%s",data); printf("\t atol test:%ld\n",k); } } } } }
•
•
•
•
Hi all,
I need ur help..
Actually I have to read the data file which containing character and digits. I need to converts some characters into digit.
The data file is like:
A11111
B22222
B33333
B4444
A55555B66666
B77777
So far my programming is like this:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define EOL '\n'
#define N 100
void OpenFile(void);
void ReadFile(void);
void ConvData(void);
char data[N];
FILE *dataptr;
unsigned int i,ii;
char *data2=malloc(strlen(data));
long k;
main()
{
clrscr();
OpenFile();
if (dataptr!=0)
{
ReadFile();
printf("read\n");
fclose(dataptr); }
{
ConvData(); }
return 0;
}
void OpenFile(void)
{
dataptr=fopen("data.dat","r");
if (dataptr==0){
printf("ERROR - Cannot open the file\n");
}
return;
}
void ReadFile(void)
{
printf("DATA FILE:\n");
while (!feof(dataptr))
{
printf("%-40s",data);
fscanf(dataptr,"%s",data);
}
}
void ConvData(void)
{ dataptr=fopen("data.dat","r");
for(i=0;i<=6;i++){
fscanf(fptdrill,"%s",data);
printf("\ndata[%i]=%s",i,data);
}
if (data2!=NULL){
memset(data2, 0, sizeof data);
{ for (ii = 0, i = 0; i < strlen(data); i++)
if (isdigit(data[i]))
data2[ii++] = data[i];
}
for (i=0;i<=6;i++)
{
k = atol(data2);
{
fscanf(fptdrill,"%s",data);
printf("\t atol test:%ld\n",k);
}
}
}
}
}
The display result is like:
data[0]=A11111 atol test=77777
data[1]=B22222 atol test=77777
data[2]=B33333 atol test=77777
....
data[6]=B77777 atol test=77777
The display result is not satisfied for me. Actually I need to display the result something like this:
data[0]=A11111 atol test=11111
data[1]=B22222 atol test=22222
data[2]=B33333 atol test=...
data[3]=B44440 ---(need to add zero)
data[4]=A55555---(B66666 need to be entered to the next line)
data[5]=B66666....
data[6]=B77777 ....
...
Please help me...
Last edited by WaltP; Mar 6th, 2007 at 1:10 am. Reason: Added CODE tags again -- why did you QUOTE your entire last post?
•
•
•
•
C Syntax (Toggle Plain Text)
char data[N]; FILE *dataptr; unsigned int i,ii; char *data2=malloc(strlen(data)); long k;
char *data2=(char*)malloc(strlen(data));
The above simply casts the pointer returned by malloc to the memory allocated to a type char.
•
•
•
•
C Syntax (Toggle Plain Text)
void OpenFile(void) { dataptr=fopen("data.dat","r"); if (dataptr==0){ printf("ERROR - Cannot open the file\n"); } return; }
if ((dataptr=fopen("data.dat", "r")) == NULL) {
....Error information
}
Those are some of the obvious things. Anyway let me know if you still have problems.
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 10:57 pm.
>The above simply casts the pointer returned by malloc to the memory allocated to a type char.
A cast is not necessary.
http://www.cprogramming.com/faq/cgi-...&id=1043284351
[edit] Nevermind, stdlib.h was included, but you still don't have to as long as you're using standard C. However, it's important that you know why casting is generally bad, because it actually removes compiler errors that could let you know of a typo or mistake somewhere in your code.
Additionally, this thread discusses the casting of malloc in greater depth:
http://cboard.cprogramming.com/showthread.php?t=25799
A cast is not necessary.
http://www.cprogramming.com/faq/cgi-...&id=1043284351
[edit] Nevermind, stdlib.h was included, but you still don't have to as long as you're using standard C. However, it's important that you know why casting is generally bad, because it actually removes compiler errors that could let you know of a typo or mistake somewhere in your code.
Additionally, this thread discusses the casting of malloc in greater depth:
http://cboard.cprogramming.com/showthread.php?t=25799
Last edited by John A; Mar 5th, 2007 at 11:11 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi,
I already run the program:
char data[N];
FILE *dataptr;
unsigned int i,ii;
char *data2=(char *)malloc(strlen(data));
long k;
The error-->illegal initialization-->char *data2=(char *)malloc(strlen(data));
But if I places the original: char *data2=malloc(strlen(data)); in this loop, is looks ok!!..
But refer to my previous problem, actually i need to convert the data file --)character to digit and display it as my prevois post..
Thanks a lot...
I already run the program:
char data[N];
FILE *dataptr;
unsigned int i,ii;
char *data2=(char *)malloc(strlen(data));
long k;
The error-->illegal initialization-->char *data2=(char *)malloc(strlen(data));
But if I places the original: char *data2=malloc(strlen(data)); in this loop, is looks ok!!..
c Syntax (Toggle Plain Text)
void ConvData(void) char *data2=malloc(strlen(data)); { dataptr=fopen("data.dat","r"); for(i=0;i<=6;i++){ fscanf(dataptr,"%s",data); printf("\ndata[%i]=%s",i,data);
But refer to my previous problem, actually i need to convert the data file --)character to digit and display it as my prevois post..
Thanks a lot...
Last edited by WaltP; Mar 6th, 2007 at 1:12 am. Reason: More CODE tags -- read the background of the text box! The words are there for a reason!
•
•
•
•
Hi,
I already run the program:
char data[N];
FILE *dataptr;
unsigned int i,ii;
char *data2=(char *)malloc(strlen(data));
long k;
The error-->illegal initialization-->char *data2=(char *)malloc(strlen(data));
But if I places the original: char *data2=malloc(strlen(data)); in this loop, is looks ok!!..
void ConvData(void)
char *data2=malloc(strlen(data));
{ dataptr=fopen("data.dat","r");
for(i=0;i<=6;i++){
fscanf(dataptr,"%s",data);
printf("\ndata[%i]=%s",i,data);
But refer to my previous problem, actually i need to convert the data file --)character to digit and display it as my prevois post..
Thanks a lot...
LamaBot
Last edited by Lazaro Claiborn; Mar 6th, 2007 at 12:10 am.
>Well... I know 'char *data2=(char*)malloc(strlen(data));' isn't an illegal initialization
Just because something isn't illegal doesn't mean you should do it. The links I provided in the previous post explained the issues involved with casting. As long as you're using standard C, there's no need to cast malloc. (Although this isn't what is likely to be causing the OP's problems.)
Will the OP please repost the entire code (within code tags, please) that you're trying to compile, so we know exactly what's happening?
Just because something isn't illegal doesn't mean you should do it. The links I provided in the previous post explained the issues involved with casting. As long as you're using standard C, there's no need to cast malloc. (Although this isn't what is likely to be causing the OP's problems.)
Will the OP please repost the entire code (within code tags, please) that you're trying to compile, so we know exactly what's happening?
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Solved Threads: 0
My code:
The data file is like:
A11111
B22222
B33333
B4444
A55555B66666
B77777
The result should be like this:
data[0]=A11111 atol test=11111
data[1]=B22222 atol test=22222
data[2]=B33333 atol test=...
data[3]=B44440 ---(need to add zero)
data[4]=A55555---(B66666 need to be entered to the next line)
data[5]=B66666....
data[6]=B77777 ....
...
Thanks a lot!!
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> #define EOL '\n' #define N 100 void OpenFile(void); void ReadFile(void); void ConvData(void); char data[N]; FILE *dataptr; unsigned int i,ii; long k; main() { clrscr(); OpenFile(); if (dataptr!=0) { ReadFile(); fclose(dataptr); } { ConvData(); } return 0; } void OpenFile(void) { if ((dataptr=fopen("data.dat","r"))==NULL){ printf("ERROR - Cannot open the file\n"); } return; } void ReadFile(void) { printf("DATA FILE:\n"); while (!feof(dataptr)) { printf("%-40s",data); fscanf(dataptr,"%s",data); } } void ConvData(void) { char *data2=malloc(strlen(data)); dataptr=fopen("data.dat","r"); for(i=0;i<=6;i++){ fscanf(dataptr,"%s",data); printf("\ndata[%i]=%s",i,data); } if (data2!=NULL){ memset(data2, 0, sizeof data); { for (ii = 0, i = 0; i < strlen(data); i++) if (isdigit(data[i])) data2[ii++] = data[i]; } for (i=0;i<=6;i++) { k = atol(data2); { fscanf(dataptr,"%s",data); printf("\t atol test:%ld\n",k); } } } }
The data file is like:
A11111
B22222
B33333
B4444
A55555B66666
B77777
The result should be like this:
data[0]=A11111 atol test=11111
data[1]=B22222 atol test=22222
data[2]=B33333 atol test=...
data[3]=B44440 ---(need to add zero)
data[4]=A55555---(B66666 need to be entered to the next line)
data[5]=B66666....
data[6]=B77777 ....
...
Thanks a lot!!
Last edited by ~s.o.s~; Mar 6th, 2007 at 10:21 pm. Reason: Added code taga, learn to use them.
![]() |
Similar Threads
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- read text file (C)
- read data from file (C++)
Other Threads in the C Forum
- Previous Thread: function question
- Next Thread: Project
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






