Read access violation when attempting strcpy from file buffer to char array










-1















I've been working on an assignment to implement hashing. In it, I read through a text file called "proteins". The problem occurs when I try to copy it to another char array. Visual Studio throws a read access violation.



#include <iostream>
#include <fstream>
using namespace std;
struct arrayelement
char protein[30];
int count;
;
arrayelement proteins[40];
int main()

char buffer[30];

// open source file
ifstream fin("proteins.txt");
if (!fin) cerr << "Input file could not be openedn"; exit(1);

// loop through strings in file
while (fin >> buffer)
int index = ((buffer[0] - 65) + (2 * (buffer[strlen(buffer)-1] - 65)) % 40);
while (true)

if (proteins[index].protein == buffer) // Found

proteins[index].count++;
break;

if (proteins[index].protein[0] == 0) // Empty

strcpy(proteins[index].protein, buffer); // <-- The error in question
proteins[index].count++;
break;

index++; // Collision



// close file
fin.close();


for (int i = 0; i <= 40; i++)

cout << proteins[i].protein << "t" << proteins[i].count << "n";











share|improve this question






















  • Did you try debug this? index may be at 40 when the error occurs.

    – Matthieu Brucher
    Nov 13 '18 at 13:40











  • Also use std::string and a std::vector<arrayelement>.

    – Matthieu Brucher
    Nov 13 '18 at 13:40







  • 2





    proteins[index].protein == buffer This is not how you want to compare char arrays.

    – 0x5453
    Nov 13 '18 at 13:44











  • @0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

    – Noctimor
    Nov 13 '18 at 13:50











  • @Noctimor Because you are trying to do C when you are in C++. Use containers....

    – Matthieu Brucher
    Nov 13 '18 at 13:51















-1















I've been working on an assignment to implement hashing. In it, I read through a text file called "proteins". The problem occurs when I try to copy it to another char array. Visual Studio throws a read access violation.



#include <iostream>
#include <fstream>
using namespace std;
struct arrayelement
char protein[30];
int count;
;
arrayelement proteins[40];
int main()

char buffer[30];

// open source file
ifstream fin("proteins.txt");
if (!fin) cerr << "Input file could not be openedn"; exit(1);

// loop through strings in file
while (fin >> buffer)
int index = ((buffer[0] - 65) + (2 * (buffer[strlen(buffer)-1] - 65)) % 40);
while (true)

if (proteins[index].protein == buffer) // Found

proteins[index].count++;
break;

if (proteins[index].protein[0] == 0) // Empty

strcpy(proteins[index].protein, buffer); // <-- The error in question
proteins[index].count++;
break;

index++; // Collision



// close file
fin.close();


for (int i = 0; i <= 40; i++)

cout << proteins[i].protein << "t" << proteins[i].count << "n";











share|improve this question






















  • Did you try debug this? index may be at 40 when the error occurs.

    – Matthieu Brucher
    Nov 13 '18 at 13:40











  • Also use std::string and a std::vector<arrayelement>.

    – Matthieu Brucher
    Nov 13 '18 at 13:40







  • 2





    proteins[index].protein == buffer This is not how you want to compare char arrays.

    – 0x5453
    Nov 13 '18 at 13:44











  • @0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

    – Noctimor
    Nov 13 '18 at 13:50











  • @Noctimor Because you are trying to do C when you are in C++. Use containers....

    – Matthieu Brucher
    Nov 13 '18 at 13:51













-1












-1








-1








I've been working on an assignment to implement hashing. In it, I read through a text file called "proteins". The problem occurs when I try to copy it to another char array. Visual Studio throws a read access violation.



#include <iostream>
#include <fstream>
using namespace std;
struct arrayelement
char protein[30];
int count;
;
arrayelement proteins[40];
int main()

char buffer[30];

// open source file
ifstream fin("proteins.txt");
if (!fin) cerr << "Input file could not be openedn"; exit(1);

// loop through strings in file
while (fin >> buffer)
int index = ((buffer[0] - 65) + (2 * (buffer[strlen(buffer)-1] - 65)) % 40);
while (true)

if (proteins[index].protein == buffer) // Found

proteins[index].count++;
break;

if (proteins[index].protein[0] == 0) // Empty

strcpy(proteins[index].protein, buffer); // <-- The error in question
proteins[index].count++;
break;

index++; // Collision



// close file
fin.close();


for (int i = 0; i <= 40; i++)

cout << proteins[i].protein << "t" << proteins[i].count << "n";











share|improve this question














I've been working on an assignment to implement hashing. In it, I read through a text file called "proteins". The problem occurs when I try to copy it to another char array. Visual Studio throws a read access violation.



#include <iostream>
#include <fstream>
using namespace std;
struct arrayelement
char protein[30];
int count;
;
arrayelement proteins[40];
int main()

char buffer[30];

// open source file
ifstream fin("proteins.txt");
if (!fin) cerr << "Input file could not be openedn"; exit(1);

// loop through strings in file
while (fin >> buffer)
int index = ((buffer[0] - 65) + (2 * (buffer[strlen(buffer)-1] - 65)) % 40);
while (true)

if (proteins[index].protein == buffer) // Found

proteins[index].count++;
break;

if (proteins[index].protein[0] == 0) // Empty

strcpy(proteins[index].protein, buffer); // <-- The error in question
proteins[index].count++;
break;

index++; // Collision



// close file
fin.close();


for (int i = 0; i <= 40; i++)

cout << proteins[i].protein << "t" << proteins[i].count << "n";








c++ arrays file char strcpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:39









NoctimorNoctimor

31




31












  • Did you try debug this? index may be at 40 when the error occurs.

    – Matthieu Brucher
    Nov 13 '18 at 13:40











  • Also use std::string and a std::vector<arrayelement>.

    – Matthieu Brucher
    Nov 13 '18 at 13:40







  • 2





    proteins[index].protein == buffer This is not how you want to compare char arrays.

    – 0x5453
    Nov 13 '18 at 13:44











  • @0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

    – Noctimor
    Nov 13 '18 at 13:50











  • @Noctimor Because you are trying to do C when you are in C++. Use containers....

    – Matthieu Brucher
    Nov 13 '18 at 13:51

















  • Did you try debug this? index may be at 40 when the error occurs.

    – Matthieu Brucher
    Nov 13 '18 at 13:40











  • Also use std::string and a std::vector<arrayelement>.

    – Matthieu Brucher
    Nov 13 '18 at 13:40







  • 2





    proteins[index].protein == buffer This is not how you want to compare char arrays.

    – 0x5453
    Nov 13 '18 at 13:44











  • @0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

    – Noctimor
    Nov 13 '18 at 13:50











  • @Noctimor Because you are trying to do C when you are in C++. Use containers....

    – Matthieu Brucher
    Nov 13 '18 at 13:51
















Did you try debug this? index may be at 40 when the error occurs.

– Matthieu Brucher
Nov 13 '18 at 13:40





Did you try debug this? index may be at 40 when the error occurs.

– Matthieu Brucher
Nov 13 '18 at 13:40













Also use std::string and a std::vector<arrayelement>.

– Matthieu Brucher
Nov 13 '18 at 13:40






Also use std::string and a std::vector<arrayelement>.

– Matthieu Brucher
Nov 13 '18 at 13:40





2




2





proteins[index].protein == buffer This is not how you want to compare char arrays.

– 0x5453
Nov 13 '18 at 13:44





proteins[index].protein == buffer This is not how you want to compare char arrays.

– 0x5453
Nov 13 '18 at 13:44













@0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

– Noctimor
Nov 13 '18 at 13:50





@0x5453 Oh man, I'm too tired. That did it. After changing that to a strcmp it all runs. Thank you!

– Noctimor
Nov 13 '18 at 13:50













@Noctimor Because you are trying to do C when you are in C++. Use containers....

– Matthieu Brucher
Nov 13 '18 at 13:51





@Noctimor Because you are trying to do C when you are in C++. Use containers....

– Matthieu Brucher
Nov 13 '18 at 13:51












1 Answer
1






active

oldest

votes


















2














If you get more than 30 chars here:



while (fin >> buffer) {


... or if index >= 40 here:



strcpy(proteins[index].protein, buffer);


... the program will probably crash (Undefined behavior). Also, these char*'s will not be pointing at the same address, so the comparison will fail:



proteins[index].protein == buffer





share|improve this answer























  • Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

    – Noctimor
    Nov 13 '18 at 13:53











  • Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

    – Ted Lyngmo
    Nov 13 '18 at 14:10











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282303%2fread-access-violation-when-attempting-strcpy-from-file-buffer-to-char-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you get more than 30 chars here:



while (fin >> buffer) {


... or if index >= 40 here:



strcpy(proteins[index].protein, buffer);


... the program will probably crash (Undefined behavior). Also, these char*'s will not be pointing at the same address, so the comparison will fail:



proteins[index].protein == buffer





share|improve this answer























  • Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

    – Noctimor
    Nov 13 '18 at 13:53











  • Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

    – Ted Lyngmo
    Nov 13 '18 at 14:10
















2














If you get more than 30 chars here:



while (fin >> buffer) {


... or if index >= 40 here:



strcpy(proteins[index].protein, buffer);


... the program will probably crash (Undefined behavior). Also, these char*'s will not be pointing at the same address, so the comparison will fail:



proteins[index].protein == buffer





share|improve this answer























  • Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

    – Noctimor
    Nov 13 '18 at 13:53











  • Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

    – Ted Lyngmo
    Nov 13 '18 at 14:10














2












2








2







If you get more than 30 chars here:



while (fin >> buffer) {


... or if index >= 40 here:



strcpy(proteins[index].protein, buffer);


... the program will probably crash (Undefined behavior). Also, these char*'s will not be pointing at the same address, so the comparison will fail:



proteins[index].protein == buffer





share|improve this answer













If you get more than 30 chars here:



while (fin >> buffer) {


... or if index >= 40 here:



strcpy(proteins[index].protein, buffer);


... the program will probably crash (Undefined behavior). Also, these char*'s will not be pointing at the same address, so the comparison will fail:



proteins[index].protein == buffer






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 13:47









Ted LyngmoTed Lyngmo

2,8112317




2,8112317












  • Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

    – Noctimor
    Nov 13 '18 at 13:53











  • Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

    – Ted Lyngmo
    Nov 13 '18 at 14:10


















  • Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

    – Noctimor
    Nov 13 '18 at 13:53











  • Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

    – Ted Lyngmo
    Nov 13 '18 at 14:10

















Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

– Noctimor
Nov 13 '18 at 13:53





Yep, it was the comparison that did me in. After rewriting it, everything runs. Part of the code here is given per the assignment -- the examples we're working with are guaranteed not to be above 30, and the index given is double what's needed, to practice collision resolution. Otherwise I'd absolutely clean that stuff up too. Thanks!

– Noctimor
Nov 13 '18 at 13:53













Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

– Ted Lyngmo
Nov 13 '18 at 14:10






Great! Note: The index formula will result in 91 if the input string is "z". It'll become: index = 57 + 114 % 40 => 57 + (114 % 40)

– Ted Lyngmo
Nov 13 '18 at 14:10




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282303%2fread-access-violation-when-attempting-strcpy-from-file-buffer-to-char-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo