473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

access error?

Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.


#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */

char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&strin g[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+curr ent_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;

if (!string[0])
return 0;

printf("\n");

// *************** *************** *************** *************//
// *************** *************** *************** *************//
while (isspace(string[i]))
{
if (string[i] == '\n')
string[i] = ' ';

// *************** *************** *************** *************//
word_len++;
i++;
}

printf("%c",str ing[i]);
while (string[i] && !isspace(string[i++]))
{
printf("%c",str ing[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\ n\n",
word_wrap(strin g, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(strin g,72));
return 0;
}
Nov 14 '05 #1
3 1247

Dick D wrote:
Could someone tell me why i get an "access violation" exception when the character at string[i] == newline (0x10) and i try to replace it with a space (0x20)? (see the get_word function)

http://www.eskimo.com/~scs/C-faq/q1.32.html


Brian

Nov 14 '05 #2
Dick D wrote:
Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.
I would prefer payment in advance...
#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */
<OT> Unusual size for tabs </OT>
char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&strin g[current_len + start_line])))
{
printf("len = %d\n",len);
Please don't use tabs in posts to Usenet, not everyone uses the same tab
stops as you so it messes up the formatting.
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+curr ent_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;
I would not bother with register. Modern optimising compilers are good
at working out where to use registers for variables.
if (!string[0])
return 0;

printf("\n");

// *************** *************** *************** *************//
// *************** *************** *************** *************//
while (isspace(string[i]))
while (isspace((unsig ned char)string[i])
Since the is* routines are specified as taking positive values or EOF
and char might be signed giving you negative values.
{
if (string[i] == '\n')
string[i] = ' ';

// *************** *************** *************** *************//
word_len++;
i++;
}

printf("%c",str ing[i]);
while (string[i] && !isspace(string[i++]))
See above
{
printf("%c",str ing[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
Main returns an int and takes either 0 or 2 parameters, declare it as
such. Implicit int is no longer valid in C99

int main(void)
{
char *string =
Although the below string is not declared as const it is still not
modifiable and your compiler has sensibly decided to mark the memory it
is in as read only.

You want
char string[] =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\ n\n",
word_wrap(strin g, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(strin g,72));
return 0;
}


I suggest you google for "comp.lang. c FAQ" and read it. You will
probably find this and many more of your questions answered.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3


Dick D wrote:

Could someone tell me why i get an "access violation" exception when the
character at string[i] == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)

Thanks in Advance.

#include <ctype.h>
#include <stdio.h> /* For FILE */

char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/

static int tab_size = 5; /* size to consider tabs as */

char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */

while (0 != (len = get_word(&strin g[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+curr ent_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}

static int get_word(char *string)
{
register int i = 0, word_len = 0;

if (!string[0])
return 0;

printf("\n");

// *************** *************** *************** *************//
// *************** *************** *************** *************//
while (isspace(string[i]))
{
if (string[i] == '\n')
string[i] = ' ';

// *************** *************** *************** *************//
word_len++;
i++;
}

Besides the problems pointed out by others, there is another error in
the above loop.
The program is likely to get an access violation if 'string' does not
contain any whitespace characters, since you increment right past the
end of the string.
printf("%c",str ing[i]);
while (string[i] && !isspace(string[i++]))
{
printf("%c",str ing[i]);
word_len++;
}

printf("\n");
return word_len;
}

main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";

printf("Here's a string wrapped to 40 columns:\n\n%s\ n\n",
word_wrap(strin g, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(strin g,72));
return 0;
}


--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #4

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

Similar topics

3
23880
by: Nicola | last post by:
Hi Everyone, I am new to programming and would like to know how to open an access Report from within vb 6. I am trying to write a program to organise cross stitch threads. I have found out how to use a database table but all I want to do now is to click a command button to display this access report. Any suggestions please ?????
4
3920
by: Fabian von Romberg | last post by:
Hi, I have installed Sql Reporting Services on 2 machines, one is WIN 2000 PRO and the other one is WIN 2000 ADV. SERVER. When I try to access a report using the webbrowser, I get the following error, this happens only if try access the report on the SERVER, it I try on my local machine, it works just fine. Any help will be much appreciated. Thanks in advance, Fabian von Romberg
1
8059
by: annie | last post by:
Hi all, I have recently ported my Access 2000 app to SQL Server, keeping the Access client as the front end using linked tables. I am also using triggers on my SQL tables to trap orphan records and validate added data. My question is..
6
4765
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
49
14364
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The application is relatively big: around 200 tables, 200 forms and sub-forms, 150 queries and 150 repports, 5GB of data (SQL Server 2000), 40 users. I'm wondering what are the disadvantages of using Access as front-end? Other that it's not...
9
2535
by: Fish Womper | last post by:
I am at best a part time developer of Access databases. I use Access 2.0, as this is all my employer has on its computers. Even so, to use this ancient version requires a fairly convoluted installation procedure on each PC on which it is used. I am self-taught from the help files that come with Access 2.0 and from painful experience. I've never attended any type of training course on how to develop databases in Access. I have no idea...
0
12073
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in '/CinemaBookingSystem' Application. -------------------------------------------------------------------------------- ERROR General error Unable to open registry key 'Temporary (volatile) Jet DSN for process
8
9643
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query work from
0
2762
by: Sebastian | last post by:
Hello I develop my applications in Access 2002. My development system is running Windows XP SP2 and I have Microsoft Office XP Developer. Microsoft Office XP is at SP3. I used Inno Setup (great product) to install my applications. When the Access Runtime was needed on the system I simply ran 'accessrt.msi' from Microsoft Office Developer discs. When another version of Access (other than Access 2002) was on the system then I simply...
2
19498
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9591
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
10343
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
10331
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
9166
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...
0
6861
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
5529
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...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.