Discuss Data duplication in the C Programming forum on Dev Shed. Data duplication C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
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 );
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.
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 ..
Posts: 6,134
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 25 m 22 sec
Reputation Power: 1974
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.
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.
Posts: 56
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 ....
Posts: 6,134
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 25 m 22 sec
Reputation Power: 1974
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.
Posts: 6,134
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 25 m 22 sec
Reputation Power: 1974
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?).
Posts: 3,364
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 28 m 48 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.