473,811 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird Issue

ncf
Ok, I've been tring to resolve this issue for some time now (~1 day
which is way longer than normal for me) to no avail.

I am reading a file into a list in memory, using a "%" delimited file
format (which allows for comments on lines beginning with "#"), and
some of the messages are not correctly copied.

I believe the problem may be somewhere around the strcpy() call, but am
not sure at all.

I hope somebody out there is able to help me, no matter how cruddy my
code may be. :\
-Wes

=============== ===== MessagesFile.c= =============== ====
#include <stdio.h>
#include <string.h>

#define BUFFLEN 1024

#define same(x,y) strcmp(x,y)==0

int num_messages = 0;
char messages[][BUFFLEN] = {};

char buffer[BUFFLEN];

void ProcessLine(cha r *s) {
printf("Process Line(\"%s\")\n" ,s);

if (!strlen(s)) { /* Empty line */
} else if (s[0]=='#') { /* Comments begin with '#' */
} else if (same(s,"%")) { /* '%' is a message divider */
if (!strlen(buffer )) {
return;
}
/* Add the message */
strcpy(messages[num_messages++], buffer);

/* Clear the buffer */
int i;
for (i=0; i<BUFFLEN; i++) {
buffer[i] = '\0';
}
} else { /* Else, message */
int true_len;
if (strlen(buffer) ) {
true_len = (int)snprintf(b uffer, sizeof(buffer), "%s\n%s", buffer,
s);
} else {
true_len = (int)snprintf(b uffer, sizeof(buffer), "%s", s);
}
if (true_len > BUFFLEN) {
printf("Message %d truncated from %d bytes to %d bytes.",
num_messages+1, true_len, BUFFLEN);
}
}
}

int main()
{
FILE *myfile;

if ((myfile = fopen("messages .txt","r"))==NU LL) {
printf("Sorry, but I failed to open the file for reading.");
return 1;
}

int pos = 0;
char c;
char line[BUFFLEN] = "";
while ( (c=fgetc(myfile )) != EOF) {
if (c=='\n' || c=='\r') {
ProcessLine(lin e);
for (pos=0; pos<BUFFLEN; pos++)
line[pos] = '\0';
pos = 0;
} else {
line[pos++] = c;
}
}
if (strlen(line)) {
ProcessLine(lin e); /* first we dump whatever remaining buffer we have
into the process function */
}
ProcessLine("%" ); /* Then, for goodness sake, we make sure that
whatever buffer remaining is processed. */

fclose(myfile);

printf("\n\n=== =====\nMessages Dump\n========\ n");
int i;
for (i=0; i<num_messages ; i++) {
printf("Message[%d]=%s\n", i, messages[i]);
}

printf("Last Char Val=%d/%c",EOF,EOF);

return(0);
}

=============== ===== messages.txt (my test file) =============== =====
# comment!!!
meow mix
%
hi?
%
woot!
%
meow
%
Next message consists of exactly 1023 characters (leaving 1 for the
ending \0 :P)
%
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa abb
%
And this message is
a multi-line message.
Woot!
%
A simple message with no end delimiter

=============== ===== Output of ``gcc MessageFile.c -Wall -o MessageFile
&& time ./MessageFile && echo $? =============== =====
ProcessLine("# comment!!!")
ProcessLine("me ow mix")
ProcessLine("%" )
ProcessLine("hi ?")
ProcessLine("%" )
ProcessLine("wo ot!")
ProcessLine("%" )
ProcessLine("me ow")
ProcessLine("%" )
ProcessLine("Ne xt message consists of exactly 1023 characters (leaving
1 for the ending \0 :P)")
ProcessLine("%" )
ProcessLine("aa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaab b")
ProcessLine("%" )
ProcessLine("An d this message is")
ProcessLine("a multi-line message.")
ProcessLine("Wo ot!")
ProcessLine("%" )
ProcessLine("A simple message with no end delimiter")
ProcessLine("%" )
========
Messages Dump
========
Message[0]=meow mix
Message[1]=
Message[2]=woot!
Message[3]=meow
Message[4]=Next message consists of exactly 1023 characters (leaving 1
for the ending \0 :P)
Message[5]=aaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aabb
Message[6]=
Woot!
Message[7]=A simple message with no end delimiter
Last Char Val=-1/ÿ
real 0m0.004s
user 0m0.000s
sys 0m0.000s
0

Nov 15 '05
11 1963
ncf wrote:
Also, anyway I define `char messages[][BUFFLEN]`, I am getting an issue
with the element I'm trying to drop in at [1]. Adding lines to the
beginning of the file only changes which element is located at that
index and, consequently, dropped.

The only way I have found to stop the dropped element problem at index
[1] is to define the variable as `char messages[100][BUFFLEN]` or such,
wherein the array isn't as flexible as it once was.


You seem to think that "char messages[][BUFFLEN]" is some sort of
automatically-resizing structure. It isn't. It is effectively the
same as:

char messages[1][BUFFLEN]

ie. an array with only one row. Even less flexible than an array
with 100 rows.

Nov 15 '05 #11
ncf
Mark & Old Wolf, thanks. Yea, coming from python, the idea that an
array is 100% fixed is sort of odd to hear at first. But ok, I guess it
makes sense now.

Mark, I believe you misread my post, but that's no prob as I now see
what I'm doing wrong. (In the section where you mentioned I couldn't
strcpy() onto [1], I had mentioned that I'm defining it as the string I
quoted from the source)

Either way, I'm slightly confused about the malloc/realloc thing, but
hopefully I can find some tutorial online somewhere.

Thanks for all yoru help
-Wes

Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2081
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed to get a working system just about finished. I am building an interface for our customer service folks to use to manage registered customers and am seeing some weird behavior.
13
3812
by: Wolfgang Kaml | last post by:
Hello All, I have been researching newsgroups and knowledgebase all morning and not found a solution that would solve the problem I have. I am having an ASP or ASPX web page that implement a counter functionality and read/insert some data in a MS Access database on that Windows 2003 server. The weird part is, as long as the web page is very short in size, I can hit the refresh button and Internet Explorer will reload the page and display...
3
1279
by: Amy M | last post by:
A user is having issues with replication. The message states it can't find a database. This database is on the distributor. When I go to Enterprise manager and add that server with a valid logon (I even tried logging on as sa), and I look at the databases. They are NOT the databases that exist on that server. They are the databases that are on the client (laptop)'s SQL Server. I've reinstalled sp3 (SQL 2000), and rebooted. Went to...
10
1850
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It doesn't work OK in ANSI mode however, as it has to use MultiByteToWideChar and WideCharToMultiByte. I've discovered that the regular expression part is working fine. As far as I can tell the regular expression code is correctly parsing what it...
5
1402
by: Peter Oliphant | last post by:
I'm drawing graphics using the Graphics object one can grab in a Form's Paint event. But I'm getting a weird thing happening... These graphic shapes flicker (even if unchanged). UNLESS- I created a timer and had the timer update a Label on the form with timer interval set to 10. Now all the shapes I draw in the Paint event of the form draw WITHOUT flicker!!! What is updating a Label doing that is preventing flicker? Is it somehow...
2
1045
by: Tracker1 - Michael J. Ryan | last post by:
I have a custom WebControl that I am working on, it's weird... I have two Properties, that I save to ViewState. I know they save, and have verified this with the ViewState viewer... When a postback happens, one of the properties is there, the other is null... not sure WTF the issue is... I tried overriding the LoadViewState and SaveViewState, the save is
1
1445
by: Mark Denardo | last post by:
I recently upgraded to VS2005 and converted some projects from 1.1 to 2.0 and am seeing two weird behaviors I can't seem to resolve, and am wondering if they are bugs or something I'm forgetting to do or add. (issue 1) When setting up a panel control with border settings: <asp:Panel Backcolor="BurlyWood" Borderstyle="Ridge" BorderColor="Black" BorderWidth="1px" ... Runat="server" />
1
2455
by: elizabeth13 | last post by:
Happy New Year, all. I'm hoping someone can help with a weird text formatting issue I'm seeing only in Safari. I change my template on a monthly basis (header, colors, etc), and this is the first time I'm seeing this. I do realize that I have validation errors...they are in some roll-up-sidebar code I copied and pasted. However, that code has been there for quite some time with no problems, so I don't think that is the issue. ...
6
3374
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request. And its main role is just to read a big xml file and save each object into its embedded DB(such as gdbm).
6
2642
by: jsamdirect | last post by:
I am having a weird issue with php preg_match. I am moving to my php sites to a new server. On the old server all works well. On the new server preg_match always returns false. If I log in via ssh and run the script at the bash prompt as root preg_match works fine. Does anyone have any thoughts on what would cause this issue?
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10653
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...
1
10408
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
10137
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...
0
9211
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7674
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
6895
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
5564
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
3876
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.