If want_names is in the same director as the program you are writing, you don't need the /
if you are using the /, put the complete path name to the file
fgets reads one line at a time, google fgets and take the first link for details
If want_names is in the same director as the program you are writing, you don't need the /
if you are using the /, put the complete path name to the file
fgets reads one line at a time, google fgets and take the first link for details
I think I am getting it ( can't check that out yet, still can't open files).:
Code:while(fgets(topps_file, 4, f1)!=NULL) { while(fgets(wants_file, 4, f2)!=NULL) if((str=strcmp(topps_file, wants_file))==0) fprintf(out, "%s", str) }
I thought you were getting file names from the user using fgets. If not, I'd suggest something like this:Something along those lines. Assuming of course that you plan to run through a list of files as described earlier.Code:char *secondaryfilenames[] = { "file1", "file2", ...the rest of your list... NULL, }; ... for( x = 0; secondaryfilenames[ x ]; x++ ) { if( (fp = fopen( secondaryfilenames[ x ], "r" )) == NULL ) { printf( "Failed to open: \'%s\'.\n", secondaryfilenames[ x ] ); } else { ...read from this file and compare it against your original source file... } }
Quzah.
Hope is the first step on the road to disappointment.
Forgot to tell, name of files come from commad line.
that is why I have:
year=atoi(argv[i]);
sprintf(fn,"topps.%2d",year); // this name of the file
You realize that %2d, given a number like "9", will generate "<space>9"? If you want "09" to be generated, use %02d.
If you're at all suspicious about the filename, print it after you've failed to open it, e.g.
[edit] Also, why do you convert it to a number and then right back again to a string? You could just useCode:perror(fn);
[/edit]Code:strcpy(fn, "fopps."); strcat(fn, argv[i]);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks, I tried, it did not fix the problem, I think the problem is not in a file name, it somewhere else
I'm not entirely sure what you're trying to do. Here are my guesses.
- "want.names" (which I hope you've removed the leading slash from) has a list of files that you're trying to open one by one and process somehow.
- You're also being passed some files on the command line, as numbers, from which your program builds full filenames and processes those as well.
- Apparently you also have a "master" file, containing lines to be updated; I'm not sure how that's specified.
- Once you have this information, you open the master file, and open the other files too. Any updated lines in the other files that also occur in the master file (I'm assuming that the lines have some recognizable features and that it's just e.g. a number that changes so you can tell that a line has been updated?) are used to replace the lines in the master file.
- Then you probably want to write the resulting data out to a new file, perhaps replacing the master file.
Please, confirm or deny these guesses and then explain in detail what you're trying to do. Actual files that you're dealing with would be helpful too, if they don't contain sensitive data.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Almost
want.names- master file , contains data for n amount of years,
1975 year
156 Smith
123 Mccarthy and so on , then another year
This file was created long time ago and never was updated
12 updated files files which contain only what year it is and numbers but no last names
I need to create a new file that will look like master file but with updated info, not replace or erase data from master file.
P.S. (which I hope you've removed the leading slash from) When I am taking slash away, I am not able to open file. ????
Well, that doesn't sound too hard. Why don't you do this: loop through the lines in the master file. Remember what the last "year" line you saw was. Read all of the names for that year from the master file, and from the corresponding twelve updated files. (I'm assuming these twelve files are one per year or something?) Write out the master's data and the new data to a new file. When you're done, go on to read the next section in the master file (where sections are denoted by "year").
Something like this:
I guess the trickiest thing about that idea is that you'll have to stop processing when you see a "year" line, and then use that line for the next iteration.Code:open master file read a line from the master file (assume it's a "year" line) repeat until end of file: read lines from the file until a "year" line is found: write these lines to the output file store the "year" line for next iteration (just keep it in the array) for each of the updated files which might contain entries for this year: get lines from these files if a line is a year line, remember the number of the year otherwise, if the year matches the year we're currently printing: print this line to the ouput file as well
Well, where is the file? Is it really at the root of your filesystem?P.S. (which I hope you've removed the leading slash from) When I am taking slash away, I am not able to open file. ????
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I have all files copied in the derictories in my Debug folder, I am using visual Studio C++ 2008Well, where is the file? Is it really at the root of your filesystem?
And what if I am open my master file read an year and open corresponding one of the 12 files to that year , then compare all data for that year
then when I am on a line on next year in master file do the same, and if I do not have corresponding to this year file skip to the next year.
will it work?
And thank for your help, Yea, you told it not hard, hmm hope I will say the same in a couple of years.
Sounds like a good start . . . try it and see what happens.And what if I am open my master file read an year and open corresponding one of the 12 files to that year , then compare all data for that year
then when I am on a line on next year in master file do the same, and if I do not have corresponding to this year file skip to the next year.
will it work?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.