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...:cool:

Recommended Answers

All 12 Replies

Sorry guys, wrong post for my previous program..this is the right program:

#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))
data2[ii++] = data;
}

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...:cool:

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.

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

>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-bin/smartfaq.cgi?answer=1047673478&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

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...

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_manual/C/FUNCTIONS/malloc.html Perhaps it is you4 compiler?

LamaBot

>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?

Ok thanks all..but actually my problem is not solving yet:(

Ok thanks all..but actually my problem is not solving yet

Well then do what I just said and post your updated code.

My code:

#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!!

ooppss...sori
line 45 is
char *data2=(char*)malloc(strlen(data));

and looks ok, no error at all..i need you to help me how to get the result 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 ....

TQ so much..

>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?

I wasn't aware of the issues involed when casting a void pointer returned by malloc. Note the hesitation.... meaning I knew it wasn't an illegal instruction that doesn't mean I claimed that it is alright to use it. Stop assuming so much. If I truely believed that it is ok to use, I'd say it. :rolleyes:

LamaBot

> main()
You may as well get used to saying int main
Implicit declarations for anything are deprecated, meaning they simply won't work at some future date.

> while (!feof(dataptr))
Read this while ( fgets( data, sizeof data, dataptr ) != NULL ) Is a good way of safely reading a text file.


> char data[N];
> FILE *dataptr;
> unsigned int i,ii;
> long k;
1. Try to do this without global variables.
2. Given them better names. Having two globals (i and ii) separated by only a single letter is a recipe for disaster, as it will be hard to figure out what is wrong.

> char *data2=malloc(strlen(data));
> memset(data2, 0, sizeof data);
So if data is an array of 50 chars, and it currently has a 5 character string in it, you allocate 5 bytes, but clear 50.
This is not good.

> ConvData(); }
Adopt a much more consistent approach to positioning of braces.
You will make the code much easier to read, for both yourself, and anyone else who has to read your code to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.