Debug Assertion Failed on code trying to read/write a text file
up vote
-1
down vote
favorite
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main()
FILE *fp;
char moreRecords, addRecord;
struct student
char firstName[100], lastName[100];
int courseNumber, sectionNumber;
;
struct student s;
char newLastName[100];
long int dbsize;
errno_t g;
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
puts("Can't open selected file");
return 0;
dbsize = sizeof(s);
while (1)
system("cls");
cout << "1. Add Records" << endl; //Lets the user add their records
cout << "2. List Records" << endl; //Lets the user List all the previous records
cout << "3. Modify Records" << endl; //Lets the user modify previously entered records
cout << "4. Delete Records" << endl; //Lets the user delete preciously entered records
cout << "5. Exit Program" << endl; //Exits the program
cout << "Please make a selection above:n";
//fflush(stdin);
addRecord = _getche();
switch (addRecord)
case '1' :
fseek(fp, 0, SEEK_END);
moreRecords = 'Y';
while (moreRecords == 'Y'
system("pause");
return 0;
So when I go through the initial options, it works for 3. Modify Records and 4. Delete Records. When you enter 1,2 or 5 it immediately breaks and gives me a Debug Assertion Failed error. It says
File: minkernelcrtsucrtsrcappcrtstdiofseek.cpp
Line: 177
Expression: stream.valid()
Tried debugging the code but wasn't able to find anything giving that error. Any help would be greatly appreciated.
c++
add a comment |
up vote
-1
down vote
favorite
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main()
FILE *fp;
char moreRecords, addRecord;
struct student
char firstName[100], lastName[100];
int courseNumber, sectionNumber;
;
struct student s;
char newLastName[100];
long int dbsize;
errno_t g;
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
puts("Can't open selected file");
return 0;
dbsize = sizeof(s);
while (1)
system("cls");
cout << "1. Add Records" << endl; //Lets the user add their records
cout << "2. List Records" << endl; //Lets the user List all the previous records
cout << "3. Modify Records" << endl; //Lets the user modify previously entered records
cout << "4. Delete Records" << endl; //Lets the user delete preciously entered records
cout << "5. Exit Program" << endl; //Exits the program
cout << "Please make a selection above:n";
//fflush(stdin);
addRecord = _getche();
switch (addRecord)
case '1' :
fseek(fp, 0, SEEK_END);
moreRecords = 'Y';
while (moreRecords == 'Y'
system("pause");
return 0;
So when I go through the initial options, it works for 3. Modify Records and 4. Delete Records. When you enter 1,2 or 5 it immediately breaks and gives me a Debug Assertion Failed error. It says
File: minkernelcrtsucrtsrcappcrtstdiofseek.cpp
Line: 177
Expression: stream.valid()
Tried debugging the code but wasn't able to find anything giving that error. Any help would be greatly appreciated.
c++
What do you expectfopen_s
to return on success?
– Tim Randall
Nov 8 at 20:10
1
"FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.
– Jesper Juhl
Nov 8 at 20:15
If you're used to usingfopen
then naturally you will want to see if the return value isNULL
. But remember thatNULL
is just a macro that evaluates to0
. Microsoft in their wisdom decided thatfopen_s
should return0
on success.
– Tim Randall
Nov 8 at 20:17
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main()
FILE *fp;
char moreRecords, addRecord;
struct student
char firstName[100], lastName[100];
int courseNumber, sectionNumber;
;
struct student s;
char newLastName[100];
long int dbsize;
errno_t g;
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
puts("Can't open selected file");
return 0;
dbsize = sizeof(s);
while (1)
system("cls");
cout << "1. Add Records" << endl; //Lets the user add their records
cout << "2. List Records" << endl; //Lets the user List all the previous records
cout << "3. Modify Records" << endl; //Lets the user modify previously entered records
cout << "4. Delete Records" << endl; //Lets the user delete preciously entered records
cout << "5. Exit Program" << endl; //Exits the program
cout << "Please make a selection above:n";
//fflush(stdin);
addRecord = _getche();
switch (addRecord)
case '1' :
fseek(fp, 0, SEEK_END);
moreRecords = 'Y';
while (moreRecords == 'Y'
system("pause");
return 0;
So when I go through the initial options, it works for 3. Modify Records and 4. Delete Records. When you enter 1,2 or 5 it immediately breaks and gives me a Debug Assertion Failed error. It says
File: minkernelcrtsucrtsrcappcrtstdiofseek.cpp
Line: 177
Expression: stream.valid()
Tried debugging the code but wasn't able to find anything giving that error. Any help would be greatly appreciated.
c++
#include <iostream>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main()
FILE *fp;
char moreRecords, addRecord;
struct student
char firstName[100], lastName[100];
int courseNumber, sectionNumber;
;
struct student s;
char newLastName[100];
long int dbsize;
errno_t g;
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
g = fopen_s(&fp,"studentdb.txt", "r+");
if (g == NULL)
puts("Can't open selected file");
return 0;
dbsize = sizeof(s);
while (1)
system("cls");
cout << "1. Add Records" << endl; //Lets the user add their records
cout << "2. List Records" << endl; //Lets the user List all the previous records
cout << "3. Modify Records" << endl; //Lets the user modify previously entered records
cout << "4. Delete Records" << endl; //Lets the user delete preciously entered records
cout << "5. Exit Program" << endl; //Exits the program
cout << "Please make a selection above:n";
//fflush(stdin);
addRecord = _getche();
switch (addRecord)
case '1' :
fseek(fp, 0, SEEK_END);
moreRecords = 'Y';
while (moreRecords == 'Y'
system("pause");
return 0;
So when I go through the initial options, it works for 3. Modify Records and 4. Delete Records. When you enter 1,2 or 5 it immediately breaks and gives me a Debug Assertion Failed error. It says
File: minkernelcrtsucrtsrcappcrtstdiofseek.cpp
Line: 177
Expression: stream.valid()
Tried debugging the code but wasn't able to find anything giving that error. Any help would be greatly appreciated.
c++
c++
asked Nov 8 at 20:06
Gage
1
1
What do you expectfopen_s
to return on success?
– Tim Randall
Nov 8 at 20:10
1
"FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.
– Jesper Juhl
Nov 8 at 20:15
If you're used to usingfopen
then naturally you will want to see if the return value isNULL
. But remember thatNULL
is just a macro that evaluates to0
. Microsoft in their wisdom decided thatfopen_s
should return0
on success.
– Tim Randall
Nov 8 at 20:17
add a comment |
What do you expectfopen_s
to return on success?
– Tim Randall
Nov 8 at 20:10
1
"FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.
– Jesper Juhl
Nov 8 at 20:15
If you're used to usingfopen
then naturally you will want to see if the return value isNULL
. But remember thatNULL
is just a macro that evaluates to0
. Microsoft in their wisdom decided thatfopen_s
should return0
on success.
– Tim Randall
Nov 8 at 20:17
What do you expect
fopen_s
to return on success?– Tim Randall
Nov 8 at 20:10
What do you expect
fopen_s
to return on success?– Tim Randall
Nov 8 at 20:10
1
1
"
FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.– Jesper Juhl
Nov 8 at 20:15
"
FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.– Jesper Juhl
Nov 8 at 20:15
If you're used to using
fopen
then naturally you will want to see if the return value is NULL
. But remember that NULL
is just a macro that evaluates to 0
. Microsoft in their wisdom decided that fopen_s
should return 0
on success.– Tim Randall
Nov 8 at 20:17
If you're used to using
fopen
then naturally you will want to see if the return value is NULL
. But remember that NULL
is just a macro that evaluates to 0
. Microsoft in their wisdom decided that fopen_s
should return 0
on success.– Tim Randall
Nov 8 at 20:17
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215365%2fdebug-assertion-failed-on-code-trying-to-read-write-a-text-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What do you expect
fopen_s
to return on success?– Tim Randall
Nov 8 at 20:10
1
"
FILE *fp;
" in a C++ program? Why? We have better ways than that old C stuff.– Jesper Juhl
Nov 8 at 20:15
If you're used to using
fopen
then naturally you will want to see if the return value isNULL
. But remember thatNULL
is just a macro that evaluates to0
. Microsoft in their wisdom decided thatfopen_s
should return0
on success.– Tim Randall
Nov 8 at 20:17