C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 11th, 2012, 04:13 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Post Data duplication

i have a problem in reading in my data textfile when it have a newline that no data it duplicate the last data that it reads sample of data ..
**********************
1 name addres
2
**********************
the output will be

name address
name address

here's the some source

void data1(FILE *fin, FILE *fout ){
int num;
int i = 1; //for loop
char buff[MAX_LEN + 1];
Amt1 = 0;

acct[0] = NULL;
while(!feof(fin)){
num = fread(buff, sizeof(char ), MAX_LEN, fin );

substring(146, 152, buff, name, sizeof name);
substring(152, 154, buff, address, sizeof address);
buff[num * sizeof(char )] = '\0';

//Account data
printf(" %s %s\n",name,address);
fprintf(fptr1," %s %s\n",name,address,);
}
}

Reply With Quote
  #2  
Old September 11th, 2012, 05:05 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 14 m 9 sec
Reputation Power: 1774
You're still not getting the hang of tagging code are you?

With [code][/code] tags, it looks like this:
Code:
int main ( ) {
    return 0;
}


Without, it looks like this:
int main ( ) {
return 0;
}

Why are you using fread() to read a text file anyway?

> while(!feof(fin))
read this
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old September 11th, 2012, 09:34 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
Ditto about choice of input function for a text file.

Consider your loop:
Code:
    while(!feof(fin))
    {
        num = fread(buff, sizeof(char ), MAX_LEN, fin );

        substring(146, 152, buff, name, sizeof name);
        substring(152, 154, buff, address, sizeof address);
        buff[num * sizeof(char )] = '\0';

        //Account data
        printf(" %s %s\n",name,address);
        fprintf(fptr1," %s %s\n",name,address,);	
    }

feof() won't return true until an attempt to read from the file has failed. The way you're doing it, you make that final attempt to read past EOF, which fails. Since it failed, it never changed the buffer which still contains the last data successfully read in. But instead of testing for success and only processing the data from a successful read, you are processing the data from the previous read, the last successful one. Hence the "duplication" of the last data.
Comments on this post
b49P23TIvg agrees: Also, always assume the cup you pick up is full. I didn't. Time to buy a new keyboard!
lhon12006 agrees!

Reply With Quote
  #4  
Old September 11th, 2012, 09:22 PM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by salem
You're still not getting the hang of tagging code are you?

i used fread to Read block of data from stream

> while(!feof(fin))
read this


its not working .. i read the links but nothing happens it duplicate again the data .. and i have sumthing i do inside the while .... to used the substring its that posible to interrupt it ..


Reply With Quote
  #5  
Old September 11th, 2012, 09:41 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
"its not working"

What changes did you make? Why are you expecting us to be able to read your mind?

Did you test whether the read worked before you attempted to process the data input? As I had suggested?

Reply With Quote
  #6  
Old September 12th, 2012, 12:14 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by dwise1_aol
"its not working"

What changes did you make? Why are you expecting us to be able to read your mind?

Did you test whether the read worked before you attempted to process the data input? As I had suggested?


yes i made it thanks sir .... just think and analyze the problem

Reply With Quote
  #7  
Old September 12th, 2012, 01:18 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
Here's an idea: RTFM! ("Read the Manual") Windows/DOS compilers usually come with help files; consult them. UNIX/Linux systems usually come with man pages, invoked from a shell command line with the man command. Lacking those, you can Google on man page function-name (substituting the name of the function in question for function-name).

Many functions have return values. You need to read the documentation for those functions to see what those return values are and to think of how you could use them. And, indeed, you at least knew enough to realize that fread returns the number of bytes read, but did you read up on what it returns when the read fails? From the fread man page at http://linux.die.net/man/3/fread:
Quote:
Return Value

fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

You're using each return value from fread as if it were non-zero (ie, the read succeeded). You also need to use it to see whether nothing was read in.

Here's a stupid thought (written just today):
Code:
    while(1)
    {
        fgets(buffer, 99, fp);
        if (feof(fp))
        {
            fclose(fp);
            break;
        }
        ParseRecord(&stars[count], buffer);
        count++;
    }

Notice how right after I perform the read, I test feof and break out of the loop if it failed. Then only after I had eliminated the possibility that it had failed do I process that input. You could also test the return value of fread for being non-zero (success) in which case you would process the input, and then if it's zero you could close the file and exit the loop.

The main thing is that you learn to think through what's happening.

Reply With Quote
  #8  
Old September 12th, 2012, 03:26 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
how about sir in working with the path for
example
you have to check for the data that i read i one folder if the data is there continue to process your program else terminate ..
and i want to use or import some com file inside my program can you give me sum example or links .. thanks a lot for your help ....

Reply With Quote
  #9  
Old September 12th, 2012, 03:43 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
OK, are we hitting a human language barrier here?
Quote:
you have to check for the data that i read i one folder if the data is there continue to process your program else terminate ..

I am a native speaker of English. I also have learned German, French, Spanish, Italian, Russian, and a half-dozen others. That line of yours doesn't make any sense at all.

Quote:
and i want to use or import some com file inside my program can you give me sum example or links


Again, I don't know what you're trying to say there.

Please use Standard English. No abbreviations. The abbreviations that work for your native language may be unintelligible in my own.

I am long over-due for going to bed. If you cannot response within the next few minutes, then I'm done for the night.

Reply With Quote
  #10  
Old September 12th, 2012, 04:33 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
i want to import the filename.com in my program in C language what should i do or give me some hint to read in importing some other file

sorry for misunderstand

Reply With Quote
  #11  
Old September 12th, 2012, 04:47 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
Quote:
Originally Posted by lhon12006
i want to import the filename.com in my program in C language what should i do or give me some hint to read in importing some other file

sorry for misunderstand


Whiskey-Tango-Foxtrot-Oscar!???

I have absolutely not idea what you are talking about.

Remember back to just before 1980. Before PC-DOS, the operating system that Microsoft had promised to IBM, there was Digital Research's CP/M, "Command Program/Microprocessor", which was the primary operating system for microprocessor systems. Their executable format was the .COM file, which MS-DOS continued to honor.

So then what? You now want to somehow load and execute an ancient executable file format? Whiskey-Tango-Foxtrot-Oscar!??? (what the ****, over?).

Just what exactly are you trying to do here?

Reply With Quote
  #12  
Old September 13th, 2012, 12:22 AM
lhon12006 lhon12006 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 56 lhon12006 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
sorry this is what i mean sir whats wrong with my project i cant access the path of file

char data[7], link1[30],*link2, *link, *data1;
char first[] = "c:\\folder\\", fname[] = "\\myfile.txt";
path[] = first;
int main()
{
printf("\n Enter First Date : ",data);
scanf("%s",&data);
if (!data){
printf("No DATA Read");
exit(0);
}
data1 = data;
strcat(date, fname);
strcat(first,date);
printf("path\t %s",first);
}

it cannot read the path .. what i do???

Reply With Quote
  #13  
Old September 13th, 2012, 07:43 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
Code:
#include<stdio.h>		/* printf and others declared */
#include<stdlib.h>		/* exit declaration */

char data[7], link1[30],*link2, *link, *data1; 

char first[] = "c:\\folder\\", fname[] = "\\myfile.txt";

/*****************path[] = first;****path has no data type************/


int main() {
  int test;  /* a new variable */
  printf("\n Enter First Date : "/*****************,data****unused by the format string************/);
  test = scanf("%s",/*****************&*******data is already a pointer*********/data);
  if (!test/*****************!data********this looks like python, not c********/){
    printf("No DATA Read");
    exit(1);
  }
  data1 = data;
  /*****************strcat(date********date is undeclared.  If you meant data instead, data has space for 7 characters and fname is longer than that*******, fname);*/
  /*****************strcat(first,date);****first is not long enough to hold more characters****************/
  printf("path\t %s",first);
  exit(0);
}
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : September 13th, 2012 at 07:45 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Data duplication

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap