User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,706 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 2,359 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.
Please support our C advertiser:
Views: 1671 | Replies: 12
Reply
Join Date: Feb 2007
Posts: 7
Reputation: ctrohana is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ctrohana ctrohana is offline Offline
Newbie Poster

help me to read my data file:((

  #1  
Mar 5th, 2007
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define EOL '\n'
  7. #define N 100
  8.  
  9. void OpenFile(void);
  10. void ReadFile(void);
  11. void ConvData(void);
  12.  
  13. char data[N];
  14. FILE *dataptr;
  15. unsigned int i,ii;
  16. char *data2=malloc(strlen(data));
  17. long k;
  18.  
  19. main()
  20. {
  21. clrscr();
  22. OpenFile();
  23. if (dataptr!=0)
  24. {
  25. ReadFile();
  26. printf("read\n");
  27. fclose(dataptr); }
  28. {
  29. ConvData(); }
  30. return 0;
  31. }
  32.  
  33. void OpenFile(void)
  34. {
  35. dataptr=fopen("data.dat","r");
  36. if (dataptr==0){
  37. printf("ERROR - Cannot open the file\n");
  38. }
  39. return;
  40. }
  41.  
  42. void ReadFile(void)
  43. {
  44. printf("DATA FILE:\n");
  45. while (!feof(dataptr))
  46. {
  47. printf("%-40s",data);
  48. fscanf(dataptr,"%s",data);
  49. }
  50. }
  51.  
  52. void ConvData(void)
  53. { dataptr=fopen("data.dat","r");
  54.  
  55. for(i=0;i<=6;i++){
  56. fscanf(fptdrill,"%s",data);
  57. printf("\ndata[%i]=%s",i,data);
  58. }
  59. if (data2!=NULL){
  60. memset(data2, 0, sizeof data);
  61. { for (ii = 0, i = 0; i < strlen(data); i++)
  62. if (isdigit(data[i]))
  63. data2[ii++] = data[i];
  64. }
  65.  
  66. for (i=0;i<=6;i++)
  67. {
  68. k = atol(data2);
  69. {
  70. fscanf(fptdrill,"%s",data);
  71. printf("\t atol test:%ld\n",k);
  72. }
  73.  
  74. }
  75. }
  76.  
  77. }
  78. }
  79.  
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:09 am. Reason: Added CODE tags -- did you miss the letters on the background of the input box of your post?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Posts: 7
Reputation: ctrohana is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ctrohana ctrohana is offline Offline
Newbie Poster

Re: help me to read my data file:((

  #2  
Mar 5th, 2007
Sorry guys, wrong post for my previous program..this is the right program:

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #define EOL '\n'
  8. #define N 100
  9.  
  10. void OpenFile(void);
  11. void ReadFile(void);
  12. void ConvData(void);
  13.  
  14. char data[N];
  15. FILE *dataptr;
  16. unsigned int i,ii;
  17. char *data2=malloc(strlen(data));
  18. long k;
  19.  
  20. main()
  21. {
  22. clrscr();
  23. OpenFile();
  24. if (dataptr!=0)
  25. {
  26. ReadFile();
  27. printf("read\n");
  28. fclose(dataptr); }
  29. {
  30. ConvData(); }
  31. return 0;
  32. }
  33.  
  34. void OpenFile(void)
  35. {
  36. dataptr=fopen("data.dat","r");
  37. if (dataptr==0){
  38. printf("ERROR - Cannot open the file\n");
  39. }
  40. return;
  41. }
  42.  
  43. void ReadFile(void)
  44. {
  45. printf("DATA FILE:\n");
  46. while (!feof(dataptr))
  47. {
  48. printf("%-40s",data);
  49. fscanf(dataptr,"%s",data);
  50. }
  51. }
  52.  
  53. void ConvData(void)
  54. { dataptr=fopen("data.dat","r");
  55.  
  56. for(i=0;i<=6;i++){
  57. fscanf(dataptr,"%s",data);
  58. printf("\ndata[%i]=%s",i,data);
  59. }
  60. if (data2!=NULL){
  61. memset(data2, 0, sizeof data);
  62. { for (ii = 0, i = 0; i < strlen(data); i++)
  63. if (isdigit(data[i]))
  64. data2[ii++] = data[i];
  65. }
  66.  
  67. for (i=0;i<=6;i++)
  68. {
  69. k = atol(data2);
  70. {
  71. fscanf(dataptr,"%s",data);
  72. printf("\t atol test:%ld\n",k);
  73. }
  74.  
  75. }
  76. }
  77.  
  78. }
  79. }
  80.  

Originally Posted by ctrohana View Post
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?
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: help me to read my data file:((

  #3  
Mar 5th, 2007
Originally Posted by ctrohana View Post
  1. char data[N];
  2. FILE *dataptr;
  3. unsigned int i,ii;
  4. char *data2=malloc(strlen(data));
  5. 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.

Originally Posted by ctrohana View Post
  1. void OpenFile(void)
  2. {
  3. dataptr=fopen("data.dat","r");
  4. if (dataptr==0){
  5. printf("ERROR - Cannot open the file\n");
  6. }
  7. return;
  8. }
  9.  

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.
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,459
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 267
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: help me to read my data file:((

  #4  
Mar 5th, 2007
>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
Last edited by John A : Mar 5th, 2007 at 10:11 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Feb 2007
Posts: 7
Reputation: ctrohana is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ctrohana ctrohana is offline Offline
Newbie Poster

Re: help me to read my data file:((

  #5  
Mar 5th, 2007
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!!..
  1. void ConvData(void)
  2.  
  3. char *data2=malloc(strlen(data));
  4. { dataptr=fopen("data.dat","r");
  5. for(i=0;i<=6;i++){
  6. fscanf(dataptr,"%s",data);
  7. printf("\ndata[%i]=%s",i,data);
  8.  

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!
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: help me to read my data file:((

  #6  
Mar 5th, 2007
Originally Posted by ctrohana View Post
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.
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,459
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 267
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: help me to read my data file:((

  #7  
Mar 5th, 2007
>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?
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Feb 2007
Posts: 7
Reputation: ctrohana is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ctrohana ctrohana is offline Offline
Newbie Poster

Re: help me to read my data file:((

  #8  
Mar 6th, 2007
Ok thanks all..but actually my problem is not solving yet
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,459
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 267
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: help me to read my data file:((

  #9  
Mar 6th, 2007
Originally Posted by ctrohana View Post
Ok thanks all..but actually my problem is not solving yet

Well then do what I just said and post your updated code.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Feb 2007
Posts: 7
Reputation: ctrohana is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ctrohana ctrohana is offline Offline
Newbie Poster

Re: help me to read my data file:((

  #10  
Mar 6th, 2007
My code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define EOL '\n'
  7. #define N 100
  8. void OpenFile(void);
  9. void ReadFile(void);
  10. void ConvData(void);
  11. char data[N];
  12. FILE *dataptr;
  13. unsigned int i,ii;
  14. long k;
  15. main()
  16. {
  17. clrscr();
  18. OpenFile();
  19. if (dataptr!=0)
  20. {
  21. ReadFile();
  22. fclose(dataptr); }
  23. {
  24. ConvData(); }
  25. return 0;
  26. }
  27. void OpenFile(void)
  28. {
  29. if ((dataptr=fopen("data.dat","r"))==NULL){
  30. printf("ERROR - Cannot open the file\n");
  31. }
  32. return;
  33. }
  34. void ReadFile(void)
  35. {
  36. printf("DATA FILE:\n");
  37. while (!feof(dataptr))
  38. {
  39. printf("%-40s",data);
  40. fscanf(dataptr,"%s",data);
  41. }
  42. }
  43. void ConvData(void)
  44. {
  45. char *data2=malloc(strlen(data));
  46. dataptr=fopen("data.dat","r");
  47. for(i=0;i<=6;i++){
  48. fscanf(dataptr,"%s",data);
  49. printf("\ndata[%i]=%s",i,data);
  50. }
  51. if (data2!=NULL){
  52. memset(data2, 0, sizeof data);
  53. { for (ii = 0, i = 0; i < strlen(data); i++)
  54. if (isdigit(data[i]))
  55. data2[ii++] = data[i];
  56. }
  57. for (i=0;i<=6;i++)
  58. {
  59. k = atol(data2);
  60. {
  61. fscanf(dataptr,"%s",data);
  62. printf("\t atol test:%ld\n",k);
  63. }
  64. }
  65. }
  66. }

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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 1:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC