473,803 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid duplicates at the end of the file during read

Andr3w
42 New Member
Hi,

I was working on something when I noticed that the following code produced a duplicate char before reaching at the end of the file if it had a blank line (with no chars at the file) before the end of the file and without it, it would run file.

Expand|Select|Wrap|Line Numbers
  1. int ReadFromFile(int argc, char **argv)
  2. {
  3.     // our file pointer, initialized at NULL
  4.     FILE *fStream = NULL;
  5.  
  6.     char buf[10];
  7.     char p;
  8.     int i = 0;
  9.  
  10.     if( (fStream = fopen(argv[1], "r")) == NULL )
  11.     {
  12.         // error, return the function
  13.         return FALSE;
  14.     }
  15.  
  16.         while( !feof( fStream ) )
  17.         {
  18.             fscanf(fStream, "%c", &p);
  19.  
  20.  
  21.             if(p != '\n')
  22.             {
  23.                 buf[i] = p;
  24.                 i++;
  25.             }else {
  26.                 buf[i] = '\0';
  27.                 i = 0;
  28.             }
  29.  
  30.             printf("%c", p);
  31.  
  32.         }
  33.  
  34.  
  35.     return TRUE;
  36. }
  37.  
The input file has the following lines

lol
asdf
mmd
(here was the blank line)

the output was:

lol
asdf
mmdd

if the blank line was present, when it didn't have that line the output was
lol
asdf
mmd

Why does this happen I have an idea but I've tried to make a workaround and came at a dead end... Any help?

Update: found a workaround but still I can't explain to myself why the above code fails to provide the output I like... The workaround is the following code

Expand|Select|Wrap|Line Numbers
  1. int ReadFromFile(int argc, char **argv)
  2. {
  3.     // our file pointer, initialized at NULL
  4.     FILE *fStream = NULL;
  5.  
  6.     char strBuf[20];
  7.     char intBuf[10];
  8.     char p;
  9.     int i = 0, j = 0;
  10.  
  11.     if( (fStream = fopen(argv[1], "r")) == NULL )
  12.     {
  13.         // error, return the function
  14.         return FALSE;
  15.     }
  16.  
  17.         do
  18.         {
  19.             p = fgetc(fStream);
  20.  
  21.             strBuf[i] = p;
  22.             i++;
  23.  
  24.                 if(p == '\n' || p == EOF)
  25.                 {
  26.                     strBuf[i-1] = '\0';
  27.                     i = 0;
  28.                 }
  29.  
  30.         }while( !feof(fStream) );
  31.  
  32.  
  33.  
  34.     return TRUE;
  35. }
  36.  
I am sure the problem lies with fscanf but I can't figure why this is happening...
Mar 4 '08 #1
1 1740
oler1s
671 Recognized Expert Contributor
Try answering a few questions for me, and we'll see where to go from there.

1. How does feof work? How do you know it works like that?

2. For fscanf, you require a character. What if someone types in value fit for a double instead? What would fscanf do? How do you know that fscanf did not receive something that could be translated as a character?

3. Buffer takes in 9 characters, right? What if someone types in 21 chars, and then hits enter. What happens in your code?

4. Are you aware of the function fgets? What does it do?

5. Yet another question: what is the return value of fgetc? Why is it so?
Mar 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
7820
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates in this array and the number of times it has been duplicated. can someone help?
6
2407
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec Qty Invoice# Item Supplier Status POReceivedDate 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
19
2901
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business layer, and the business layer is coupled to the data layer. So far so good. Suppose the data layer raises an event, and it passes Me (the sender) as an object, and e (MyEventArgs, a descendent of EventArgs) to the layer above (the business...
6
6526
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST data will be resubmitted), the previously submitted record is sumbitted again, and a duplicate record is inserted into the table. In PHP I would have avoided this by submitting the form to a processing page, which would then automatically...
13
11159
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
1
1664
by: pgrayove | last post by:
I'm a first time poster and I would really appreciate any help. I'm working with a MySQL database using a Perl script to access the data and display as a web page. I'm stuck with one part. The database has repeated events in it. These events have the same title, but different dates/times. I would like to select all of the dates and times for each event, but only print each title one time. Right now I'm getting something like this: Alef...
1
2339
by: Anghared | last post by:
I used to study VB in college but it's been a while. Ironically I've forgotten most of it just as I've found the perfect opportunity to utilize it. I want the application to read lines of text in a textbox (names of students), scan a txt file to see if the names are on the list already, then display in a second textbox all the names which are new. Then write the new names to the txt file so that they won't be counted twice next time. ...
16
3524
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
4
6387
by: ryushinyama | last post by:
I had to do a lot of searching to get this one to work and in doing so I saw a lot of different sites where people were looking for this answer so I thought I would put it up. If you are trying to put data in to a table where you don't want any duplicate data a primary key, unique constraint and all that prevention is the best way to go. However when you try to insert data and it conflicts with an entry you get an error then you have to...
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.