•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 401,538 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,437 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1680 | Replies: 12
![]() |
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Rep Power: 0
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 12: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:
Rep Power: 0
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 12:10 am. Reason: Added CODE tags again -- why did you QUOTE your entire last post?
•
•
•
•
cplusplus Syntax (Toggle Plain Text)
char data[N]; FILE *dataptr; unsigned int i,ii; char *data2=malloc(strlen(data)); long k;
Try to allocate the memory using malloc like this:
char *data2=(char*)malloc(strlen(data));
The above simply casts the pointer returned by malloc to the memory allocated to a type char.
•
•
•
•
cplusplus Syntax (Toggle Plain Text)
void OpenFile(void) { dataptr=fopen("data.dat","r"); if (dataptr==0){ printf("ERROR - Cannot open the file\n"); } return; }
On an unsuccessful file open NULL is returned from fopen. You can do this for instance:
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 9: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 10:11 pm.
tuxation.com - Linux articles, tutorials, and discussions
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Rep Power: 0
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 12: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...
Well... I know 'char *data2=(char*)malloc(strlen(data));' isn't an illegal initialization as shown here: http://www.space.unibe.ch/comp_doc/c...NS/malloc.html Perhaps it is you4 compiler?
LamaBot
Last edited by Lazaro Claiborn : Mar 5th, 2007 at 11:10 pm.
>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?
tuxation.com - Linux articles, tutorials, and discussions
•
•
Join Date: Feb 2007
Posts: 7
Reputation:
Rep Power: 0
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 9:21 pm. Reason: Added code taga, learn to use them.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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



Linear Mode