Why am I getting a segmentation fault error? - c++

I am receiving a segmentation fault and am unsure what that means and cant find whats wrong?
Pico is giving this error,
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
//Struct groups each line in the file
struct gradesRecord
{
int iIndex; // index on the file
int iStudentNUM; // 'Student' field
int iExamGrouped[3]; // 'Exam 1'..'Exam 3' fields
char cStudentAVG; // 'Grade' field
};
void printUnsortedStringFromFile(int amount, struct gradesRecord A[]);
void printSortedStringFromFile(int amount, struct gradesRecord A[]);
//bool binSearchNUM(int amount, int A[amount], int target,);
int main()
{
FILE* spData = fopen("grades.csv", "r");
int ch, number_of_lines = 0;
do
{
ch = fgetc(spData);
if (ch == '\n')
number_of_lines++;
} while (ch != EOF);
if (ch != '\n' && number_of_lines != 0)
number_of_lines++;
fclose(spData);
printf("There are %d lines in file grades.csv . \n", number_of_lines);
int amount = number_of_lines;
struct gradesRecord A[amount];
printUnsortedStringFromFile(amount, A );
printSortedStringFromFile(amount, A );
//binSearchNUM(int amount, int A[amount], int target, );
return 0;
}
/*
* Function Name: printUnsortedStringFromFile
*
* Input Parameters: takes array A
*
* Description: This fuction prints the original list that was unsorted in grades.csv
*
* Return Value: void
*/
void printUnsortedStringFromFile(int amount, struct gradesRecord A[])
{
FILE *spData;
spData = fopen("grades.csv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file grades.csv.\n");
exit(1);
}
printf("+-------+------+------+------+-----+\n");
printf("|Student|Exam 1|Exam 2|Exam 3|Grade|\n");
printf("+-------+------+------+------+-----+\n");
char sLine[81]; //local string to read one row
int j = 0; //storage index
while((fgets(sLine, 80, spData)) != NULL)
{
sscanf(sLine, "%d, %d, %d, %d, %c",
&(A[j].iStudentNUM), &(A[j].iExamGrouped[0]), &(A[j].iExamGrouped[1]), &(A[j].iExamGrouped[2]),
&(A[j].cStudentAVG));
printf("|%7d| %5d| %5d| %5d| %c| \n",
A[j].iStudentNUM, A[j].iExamGrouped[0], A[j].iExamGrouped[1], A[j].iExamGrouped[2],
A[j].cStudentAVG);
j++; // next row
}
printf("+-------+------+------+------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file grades.csv. \n");
exit(2);
}
}
/*
* Function Name: printSortedStringFromFile
*
* Input Parameters: takes int amount, struct gradesRecord A
*
* Description: This function prints the sorted version of the file grades.csv omitting
* the exam values and giving each string a index number
*
* Return Value: void
*/
void printSortedStringFromFile(int amount, struct gradesRecord A[])
{
FILE *spData;
spData = fopen("grades.csv", "r");
if(spData == NULL)
{
fprintf(stderr, "Error opening the file grades.csv.\n");
exit(1);
}
char sLine[81];
int iLine = 0, iRow;
struct gradesRecord grRow, grTmp;
while((fgets(sLine, 80, spData)) != NULL)
{
// extract one Row and store it into grRow
sscanf(sLine, "%d, %d, %d, %d, %c",
&(grRow.iStudentNUM), &(grRow.iExamGrouped[0]), &(grRow.iExamGrouped[1]), &(grRow.iExamGrouped[2]),
&(grRow.cStudentAVG));
// keep the line index of that row
grRow.iIndex = iLine;
// target loop = insertion sort algorithm
for (iRow = 0; iRow < iLine; iRow++)
{
// detect if new student is before the store one
if (grRow.iStudentNUM < A[iRow].iStudentNUM)
{
// exchange both student records through grTmp
memcpy(&grTmp, &(A[iRow]), sizeof(struct gradesRecord));
memcpy(&(A[iRow]), &grRow, sizeof(struct gradesRecord));
memcpy(&grRow, &grTmp, sizeof(struct gradesRecord));
}
}
// store the biggest student at the end
memcpy(&(A[iLine]), &grRow, sizeof(struct gradesRecord));
iLine++;
}
int StudentNUM;
char StudentAVG;
int j = 0;
printf("+-----+-------+-----+\n");
printf("|Index|Student|Grade|\n");
printf("+-----+-------+-----+\n");
int index;
while (j < amount - 1)
{
StudentNUM = A[j].iStudentNUM;
StudentAVG = A[j].cStudentAVG;
index = j+1;
printf("| %4d|%7d| %c| \n", index, StudentNUM, StudentAVG);
j++;
}
printf("+-----+-------+-----+\n");
if (fclose(spData) == EOF)
{
fprintf(stderr, "Error closing the file grades.csv. \n");
exit(2);
}
}

create grades.csv with this input format:
Student,Exam 1,Exam 2,Exam 3,Grade
for example:
1,18,17,14,18
2,15,18,10,16
and output is, witout any Error, but if grades.csv not exist or have a bad information, you get Segmentation fault (core dumped)
There are 3 lines in file grades.csv .
+-------+------+------+------+-----+
|Student|Exam 1|Exam 2|Exam 3|Grade|
+-------+------+------+------+-----+
| 1| 18| 17| 14| 1|
| 2| 15| 18| 10| 1|
+-------+------+------+------+-----+
+-----+-------+-----+
|Index|Student|Grade|
+-----+-------+-----+
| 1| 1| 1|
| 2| 2| 1|
+-----+-------+-----+

Related

Reference cannot be initialized/expression must have type class

I'm doing a quick binary tree program as a homework, but I am getting weird errors that I can't seem to figure out how to fix. Normally I'm programming in C#, and C is just slightly different but different enough for me to get confused.
Here is the code that is giving the error:
void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;
do {
flag = 1;
for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);
printf("Sorted by value of variable 'b'"); _getch(); _getch();
}
And here is the whole script:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct stub
{
char crown[51];//some custom name - can be empty
float b;//comparable value for tree sort
int c;//limited by maxint
stub *l;//left node(normally smaller "b" value than this node)
stub *r;//right node(normally bigger "b" value than this node)
};
stub *stubList[255];//empty list of a stub array with a safe casual amount
int arraySize = 0;
stub *head = NULL;//main stub(first)
stub *latest = NULL;//latest stub
stub *st = NULL;//used for creating and checking
FILE *fs = NULL;
char fileName[255];
int maxint = 20;//for var 'c'
void BTreeNodeToArray(stub *s) {
stubList[arraySize] = s;
arraySize++;
}
void Swap(stub &s1, stub &s2) {
stub temp = s2;
s1 = s2;
s2 = temp;
}
int MaxMin(int num) {
int clampedInt = num;
if (num < 0) clampedInt = 0;
else if (num > maxint) clampedInt = maxint;
return clampedInt;
}
//Create a completely new stub with crown, b, and c variables being filled here
void CreateElement() {
st = new stub;
printf("Adding information for node:\n");
printf("Node name: "); gets_s(st->crown);
printf("\n");
float f;
printf("Node float value (B): "); scanf_s("%f", &f);
st->b = f;
printf("\n");
int d;
printf("Node integer value (C): "); scanf_s("%d", &d);
st->c = d;
printf("\n");
st->c = MaxMin(st->c);
st->l = NULL;
st->r = NULL;
}
//creates the very first stub(root/head)
void CreateFirst() {
printf("First in tree. Adding root node...\n");
CreateElement();
head = st;
latest = head;
BTreeNodeToArray(head);
printf("Added head stub\ncrown: %s\nValue: %f\nExtra: %d", head->crown, head->b, head->c);
getchar();
getchar();
}
void AddStub() {
if (head == NULL) {
CreateFirst();
}
else {
CreateElement();//create newest node
latest = st;
st = head;
int depth = 0;
while (1) {
if ((latest->b <= st->b)) {//choose left if true
printf("Went left\n");
depth++;
if (st->l == NULL) {//node free, assign here
printf("Node assigned at depth %d\n", depth);
st->l = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (left)\n");
st = st->l;
}
}
else {//choose right
printf("Went right\n");
depth++;
if (st->r == NULL) {//node free, assign here
printf("Node assigned at depth %d\n", depth);
st->r = latest;
BTreeNodeToArray(latest);
getchar();
getchar();
break;
}
else {//loop again with next node
//printf("New loop (right)\n");
st = st->r;
}
}
}
}
}
void ViewArray() {
for (int i = 0; i < arraySize; i++)
{
printf_s("Node [%d]:\n\tCrown: %s\n\tweight: %f\n\tExta value(0 - %d): %d\n", i, stubList[i]->crown, stubList[i]->b, maxint, stubList[i]->c);
}
getchar();
}
void SortArray() {//bubble sorting by float value of 'b'
int flag = 0;
do {
flag = 1;
for (int i = 0; i < arraySize - 1; i++)
{
if (stubList[i].b > stubList[i + 1].b) {
Swap(stubList[i], stubList[i + 1]);
flag = 0;
}
}
} while (flag == 0);
printf("Sorted by value of variable 'b'"); _getch(); _getch();
}
void ProcessArray() {
char c = ' ';
int found = 0;
printf("Process with character: \n"); c = _getch();
if (arraySize <= 0) {
printf("No List!");
return;
}
char chkstr[5];
for (short i = 0; i < 5; i++)//simple assign to a 'string' 5 times
{
chkstr[i] = c;
}
for (int i = 0; i < arraySize; i++)
{
if (strstr(stubList[i]->crown, chkstr) != NULL) {
// contains
printf("B = %f at [%d] \n", stubList[i]->b, i);
found++;
}
}
if (found > 0) {
printf("---Found: %d with character %c---", found, c);
}
else {
printf("No elements found with '%c'", c);
}
getchar();
}
void ExportArray() {
FILE *fs = NULL;
char fileName[255];
errno_t err;
printf("Save to file as: \n"); gets_s(fileName);
if (strlen(fileName) == 0) {
printf("Failed to create file. File name empty!");
_getch();
_getch();
}
err = fopen_s(&fs, fileName, "wb");
if (err != 0) {
printf("Failed to create file!!!");
_getch();
_getch();
return;
}
for (int i = 0; i < arraySize; i++)
{
int written = fwrite(&stubList[i], sizeof(stub), 1, fs);
//fwrite(&gr[i], sizeof(student), 1, fs);
}
int numclosed = _fcloseall();
printf("Exported to file: %s", fileName);
getchar();
}
void CleanReset() {//reset all values and release memory (function used for import)
for (int i = 0; i < arraySize; i++)
{
delete stubList[i];
}
arraySize = 0;
st = NULL;
head = NULL;
latest = NULL;
}
void ImportArray() {
FILE *fs = NULL;
char fname[255];
errno_t err;
printf("Open FIle: "); gets_s(fname);
err = fopen_s(&fs, fname, "rb");
if (err == 0) {
CleanReset();
fseek(fs, 0, SEEK_END);
long size = ftell(fs);
arraySize = size / sizeof(stub);//get amount of students saved
fseek(fs, 0, SEEK_SET);
int i = 0;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
while (!feof(fs)) {
i++;
st = new stub;
fread_s(&st, sizeof(stub), sizeof(stub), 1, fs);
stubList[i] = st;
}
printf("File data imported. Size: %d", arraySize);
getchar();
return;
}
printf("Failed to import file!!!");
getchar();
}
void main()
{
system("chcp 65001");//use utf8
char izb = 'i';
while (izb != '0') {
system("cls");
printf_s("1. Add stub\n");
printf_s("2. View Array\n");
printf_s("3. Sort Array\n");
printf_s("4. Process\n");
printf_s("5. Export array to file\n");
printf_s("6. Import array from file\n");
printf_s("0. Exit\n\n");
printf_s("Choose action:\n"); izb = _getch();
switch (izb)
{
case '1':
AddStub();
break;
case '2':
ViewArray();
break;
case '3':
SortArray();
break;
case '4':
ProcessArray();
break;
case '5':
ExportArray();
break;
case '6':
ImportArray();
break;
case '0':
//Exit();
break;
}
}
}
The error is that you want to pass a stub * to a function that wants a stub &. The first one is a pointer, the second one is a reference.
You have two options.
First option (imho recommended):
Change your swap function to accept pointers instead of references.
void Swap(stub *s1, stub *s2) {
stub temp = *s2;
*s1 = *s2;
*s2 = temp;
}
Second option:
Dereference the parameters before passing to swap:
Swap(*(stubList[i]), *(stubList[i + 1]));
You have to undestand that the datatype in your list is a pointer to stub, the function Swap wants a reference, which are different things in C++.
For a better understanding I recommend reading this: https://www.geeksforgeeks.org/pointers-vs-references-cpp/

How to add statement if-else?

How to add if-else statement for "kodeprodi"?
Everytime I add if-else statement, the message "Lvalue required" always appears.
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct {
char bp[13];
char nama[15];
int kodeprodi;
char namaprodi[10];
float ipk;
} mahasiswa;
int main()
{
char pil;
do {
mahasiswa mhs[10];
int i, n;
{
printf("Data Nilai Mahasiswa\n");
printf("Berapa banyak data = ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Data mahasiswa ke-%d\n", i+1);
printf("Nomor BP: "); scanf("%s", &mhs[i].bp);
printf("Nama: "); scanf("%s", &mhs[i].nama);
printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
printf("IPK: "); scanf("%f", &mhs[i].ipk);
if (mhs[i].kodeprodi == 260) {mhs[i].namaprodi = "SI";}
else if (mhs[i].kodeprodi == 261) {mhs[i].namaprodi = "TI";}
}
//output
printf("No. BP Nama Kode Prodi Nama Prodi IPK \n");
for(i = 0; i < n; i++) {
printf("\n%2d %-10s %-9s %3d %3s %3.f\n",
i+1, mhs[i].bp, mhs[i].nama, mhs[i].nama,
mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
}
}
printf("Repeat again? Y/N");
scanf("%s", &pil);
printf("\n\n");
} while ((pil == 'Y') || (pil == 'y'));
}
Even if in the statement if-else, I type like this
if(mhs[i].kodeprodi==260){namaprodi = "SI");
The error message is "Undefined symbol 'namaprodi'
I tweaked your code a bit. Got rid of unused conio.h, changed kodeprodi type to int (because char can only handle numbers -127..127), removed & from some scanf calls (because you should pass pointer to first character for %s formatter), deleted extra mhs[i].nama argument for printf.
Sorry, I completely didn't understood your code :-) My tweaks were semi-automatic! You should learn C programming better.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char bp[13];
char nama[15];
int kodeprodi;
char namaprodi[10];
float ipk;
} mahasiswa;
int main() {
char pil;
do {
mahasiswa mhs[10];
int i, n;
{
printf("Data Nilai Mahasiswa\n");
printf("Berapa banyak data = ");
scanf("%d", &n);
for(i=0;i<n;i++) {
printf("Data mahasiswa ke-%d\n", i+1);
printf("Nomor BP: "); scanf("%s", mhs[i].bp);
printf("Nama: "); scanf("%s", mhs[i].nama);
printf("Kode Prodi: "); scanf("%d", &mhs[i].kodeprodi);
printf("IPK: "); scanf("%f", &mhs[i].ipk);
if(mhs[i].kodeprodi==260)
strcpy(mhs[i].namaprodi, "SI");
else if(mhs[i].kodeprodi==261)
strcpy(mhs[i].namaprodi, "TI");
}
//output
printf("No. BP Nama Kode Prodi Nama Prodi IPK \n");
for(i=0;i<n;i++) {
printf("\n%2d %-10s %-9s %3d %3s %3.f\n", i+1, mhs[i].bp, mhs[i].nama, mhs[i].kodeprodi, mhs[i].namaprodi, mhs[i].ipk);
}
}
printf("Repeat again? Y/N");
scanf("%s", &pil);
printf("\n\n");
} while ((pil == 'Y') || (pil == 'y'));
return 0;
}
For a quick fix, use:
if(mhs[i].kodeprodi==260){strncpy(mhs[i].namaprodi, "SI", 9);
strncpy() is needed to copy the contents into namaprodi.
namaprodi is a member of struct mahasiswa, so you can't access it directly.
But better use std::string instead.
Also, as #BoPersson mentioned, char kodeprodi; can't hold 260, so you'll better to convert that to an int.

How to read csv files using 2d Array

I have done using the single dimensional array,Want to convert it using two dimensional array.As i am a beginner, I am not able to do it.Please help me in converting this program using two dimensional array and thank you in advance
#include<stdio.h>
#include<string.h>
FILE *file;
char str[20];
char buffer[128];
char *token;
char *a[20],b[20];
int main ()
{
int i=0;
printf("\n Enter the file name:");
scanf("%s",&str);
file=fopen(str,"r");
if(file != NULL)
{
while(fgets(buffer,128,file) != NULL)
{
token = strtok(buffer,",");
while(token != NULL)
{
a[i]=strdup(token);
printf("%s\t",a[i]);
token = strtok(NULL,",");
i++;
}
}
}
else
{
printf("\n the file name you have entered doesnot exist");
}
fclose(file);
return 0;
}
#include<stdio.h>
#include<string.h>
int e,f;
FILE *file;
char buffer[128];
char *token;
int n,m;
char *a[10][10];
int i=0,j;
void find_keyword()
{
while(fgets(buffer,128,file) != NULL)
{
token = strtok(buffer,",");
j=0;
while(token != NULL)
{
a[i][j]=strdup(token);
printf("a[%d][%d]=%s\t",i,j,a[i][j]);
token = strtok(NULL,",");
j++;
}
i++;
}
}
void main ()
{
printf("Enter the values of m and n?\n");
scanf("%d",&m);
scanf("%d",&n);
file=fopen("D:/test.csv","r");
printf("File Open\n");
if(file != NULL)
{
find_keyword();
}
else
{
printf("\n the file name you have entered doesnot exist");
}
fclose(file);
printf("\nFile Closed\n");
}

Reading data from multiple .txt files with c++

I need to extract a series of information from multiple files (64 files) in a directory. Each file consists of several lines like:
Pair of Operators: 0& ins: 0& rmv: 0& weight: 0.124354
Pair of Operators: 1& ins: 1& rmv: 0& weight: 0.00672458
Pair of Operators: 2& ins: 2& rmv: 0& weight: 0.000467531
...
The information is stored in a vector. I wrote a code, however, I receive a segmentation fault error. Here is my code. Could anyone indicate where the problem comes from?
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <dirent.h>
using namespace std;
struct info {
char* name;
vector<vector<double>> weights; // dest-const-weight
};
int main(int c, char* v[]) {
struct dirent* pDirent;
DIR* pDir;
char buffer[50];
strcpy(buffer, v[1]);
if (c < 2) {
printf ("Usage: testprog <dirname>\n");
return 1;
}
pDir = opendir (buffer);
if (pDir == NULL) {
printf ("Cannot open directory '%s'\n", v[1]);
return 1;
}
vector<info> Information;
for (int inst = 0; inst < 64; inst++) {
info temp;
temp.weights.resize(9);
for (int j = 0; j < 9; j++)
temp.weights[j].resize(4);
Information.push_back(temp);
}
int cmp = 0;
while ((pDirent = readdir(pDir)) != NULL) {
if (strcmp(pDirent->d_name, ".") != 0 &&
strcmp(pDirent->d_name, "..") != 0) {
char line[1024];
ifstream file(pDirent->d_name);
Information[cmp].name = pDirent->d_name;
int oper = -1;
int dest = -1;
int ins = -1;
double weight = -1;
for (int l = 0; l < 36; l++) {
file.getline(line, 1024);
// cout<<line<<endl;
sscanf(line,
"Pair of Operators: %d& ins: %d& rmv: %d& weight: %f",
&oper, &dest, &ins, &weight);
// sscanf(line, "%*s %d%*s %d%*s %d%*S %f", &oper, &dest, &ins,
// &weight);
// cout<<oper<<" "<<dest<<" "<<ins<<" "<<weight<<endl;
Information[cmp].weights[dest][ins] = weight;
}
cmp++;
}
}
closedir(pDir);
return 0;
}
I can see two obvious seg faults.
First of all you copy v[1] into buffer. Do you provide any arguments to your program?
Second one I see on the fly is that you try to call readdir on a uninitialized pointer of DIR.
Try DIR* pDir = opendir(...);
You should always print a usage message when the argument count doesnt match!
if (c != 1) {
fprintf(stderr, "USAGE: %s needs exactly one argument!\n" v[0]);
return 0;
}
One more thing I forgot to mention. You can compile your program with the -g flag and try to use a debugger to find these errors.

Color switching of an image using C++ code

Since i am quiet new to C++ and Image processing i have a problem modifying and adding a function to the code.
The requirement is only to switch between the RGB colors.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
class myImageData
{
private:
int mW;
int mH;
int mCH;
double * mData;
void SkipComments(FILE *fp)
{
int ch;
char line[100];
while ((ch = fgetc(fp)) != EOF && isspace(ch))
;
if (ch == '#')
{
fgets(line, sizeof(line), fp);
SkipComments(fp);
}
else
{
fseek(fp, -1, SEEK_CUR);
}
}
public:
myImageData(void)
{
this->mData = NULL;
}
~myImageData()
{
if (this->mData != NULL)
{
delete[] this->mData;
}
}
void init(int W, int H, int CH)
{
this->mW = W;
this->mH = H;
this->mCH = CH;
if (this->mData != NULL)
delete[] this->mData;
this->mData = new double[(this->mW)*(this->mH)*(this->mCH)];
}
int getWidth(void)
{
return this->mW;
}
int getHeight(void)
{
return this->mH;
}
int getCH(void)
{
return this->mCH;
}
double * getDataPtr(void)
{
return this->mData;
}
double get(int x, int y)
{
return this->mData[y*(this->mW) + x];
}
double get(int x, int y, int CH)
{
return this->mData[this->mCH * (y*(this->mW) + x) + CH];
}
void set(int x, int y, double value)
{
this->mData[y*(this->mW) + x] = value;
}
void set(int x, int y, int CH, double value)
{
this->mData[this->mCH *(y*(this->mW) + x) + CH] = value;
}
void read(const char *filename);
void save(const char *filename);
};
void myImageData::read(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL){
printf("Cannot open %s\n", filename);
exit(1); //abnormal termination
}
printf("Read an image from: %s\n", filename);
// read ppm/pgm header
char buf[256];
char filetype[256];
int W, H, Range, CH;
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", filetype);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d%d", &W, &H);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d", &Range);
//printf("Header: %s, %d, %d, %d\n", filetype, W, H, Range);
SkipComments(file);
if (strcmp(filetype, "P5") == 0)
{
CH = 1;
}
else if (strcmp(filetype, "P6") == 0)
{
CH = 3;
}
else
{
printf("Unknown image type\n");
exit(1); //abnormal termination
}
if (Range != 255){
printf("Invalid data\n");
exit(1); //abnormal termination
}
// create myImageData class
init(W, H, CH);
// read ppm data
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char[datalength];
fread(temp, sizeof(unsigned char), datalength, file);
double * ptr1 = this->mData;
unsigned char *ptr2 = temp;
for (int i = 0; i < datalength; i++){
*ptr1 = (double)*ptr2;
ptr1++;
ptr2++;
}
delete[] temp;
fclose(file);
}
void myImageData::save(const char *filename){
char filenamefull[256];
if (this->mCH == 1){
sprintf(filenamefull, "%s.pgm", filename);
}
else{
sprintf(filenamefull, "%s.ppm", filename);
}
FILE *file = fopen(filenamefull, "w");
printf("Write an image to: %s \n", filenamefull);
if (this->mCH == 1){
fprintf(file, "P5\n");
}
else{
fprintf(file, "P6\n");
}
fprintf(file, "%d %d\n", this->mW, this->mH);
fprintf(file, "255\n");
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char[datalength];
double * ptr1 = this->mData;
unsigned char * ptr2 = temp;
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
fwrite(temp, sizeof(unsigned char), datalength, file);
delete[] temp;
fprintf(file, "Â¥n");
fclose(file);
}
The errors i am having:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
error LNK1120: 1 unresolved externals
Firstly, you have no main function. No surprise that your code doesn't work.. all you have is a class to load, save and manipulate PPM image files.
You appear to me using Visual Studio, so you'll need a function that looks like this:
int _tmain(int argc, _TCHAR* argv[])
{
myImageData image;
image.read("atestfile.ppm");
// do some stuff to your image
image.write("outputfile.ppm");
}
I'm assuming you have a test image in PPM format you can use here, of course.
Now this is madness:
double * ptr1 = this->mData;
unsigned char * ptr2 = temp;
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
You've read from an unsigned char, so there's no point stuffing it into a double, and there's definitely no point in checking if the value lies outside of 0 to 255. Why are you storing things in doubles? It makes no sense! Even if you did do something that needed a full double-precision floating point value per channel, you throw it all away in the output when you clamp everything to 0-255 again:
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
Also, this is basically C dressed up in a thin C++ veneer. That's okay, everyone has to start somewhere. But instead of using new to create an array, you can do this:
// read ppm data
int datalength = this->mW * this->mH * this->mCH;
// using a std::vector here means that the allocated memory will be freed
// automatically, even in the result of an error occurring.
std::vector<unsigned char> temp(datalength);
fread(&temp[0], sizeof(unsigned char), datalength, file);
I'd also consider using iostream classes such as fstream instead of fread and fopen and so on. But this is not really the place to get into those kinds of details.
Anyway, What to do with your image once it is loaded? You've got dead easy helper functions to read and write pixel values, which will let you do pretty much anything you want. Here's a simple example, swapping the R and B channels. You might get something better when you actually tell us what you wanted.
void swapRB(myImageData& image)
{
assert(image.getCH() == 3);
for (int x = 0; x < image.getWidth())
{
for (int y = 0; x < image.getHeight())
{
double R = image.get(x, y, 0);
double G = image.get(x, y, 1);
double B = image.get(x, y, 2);
image.set(x, y, 0, B);
image.set(x, y, 2, R);
}
}
}