main not recognizing my function - c

#include <stdio.h>
#include <stdlib.h>
void reverse(char* lines[], int count)
{
for (int i = count-1; i >= 0; i--)
{
printf("%s", lines[i]);
}
}
.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sortutil.h"
#include "reverse.h"
int getarray(char *lines[]);
void printarray(char *lines[], int max);
int main(int argc, char* argv[])
{
char* arr[100];
int numlines = getarray(arr);
printf("There are %d lines\n", numlines);
printarray(arr, numlines);
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-s") == 0)
{
sortutil(arr);
printarray(arr, numlines);
}
if (strcmp(argv[i], "-r") == 0)
{
reverse(arr, numlines);
printarray(arr, numlines);
}
}
}
int getarray(char *lines[])
{
int i = 0;
char *text = (char *)malloc(200);
while (fgets(text, 200, stdin) != NULL)
{
lines[i] = text;
i++;
text = (char *)malloc(200);
}
return i;
}
void printarray(char *lines[], int max)
{
for (int i = 0; i < max; i++)
{
printf("%s\n\n", lines[i]);
}
}
when i compile the main function it is telling me that there is an undefined reference to 'reverse'. I did #include "reverse.h" so it shouldn't have a problem seeing the reverse function. Am I missing something

You're missing the implementation. You defined the prototype, but the function body itself is missing. It is in a separate file, and you need to tell the linker about it. When you compile your main.cc - add the other file to the command line as well.

Related

Cannot use both Argv's, cannot run Move twice segmentation fault

Ive been trying various ways to get my program to work. regardless of weather i try argv1 or argv2 first, the second one will segmentation fault. even if i try to print SOURCE2DEFINE or argv[2] AFTER a move() it will segmentation fault. i cannot move both files trying to run move twice will result in a segmentation fault. im assuming that it has to be something to do with pointers and allocation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#define SOURCEDEFINE argv[1]
#define SOURCE2DEFINE argv[2]
#define DESTDEFINE argv[argc - 1]
#define ARGCDEFINE argc
#define COMMANDDEFINE argv[0]
int getIndex(char, char*);
char* fileGetName(char*);
void move(char*, char*);
void copy(char*, char*);
int main(int argc, char** argv)
{
printf("Test Argc: %d\n", argc);
int lengthArray = argc-2;
printf("Test Length: %d\n", lengthArray);
printf(" command %s\n", COMMANDDEFINE);
printf("%s\n", DESTDEFINE);
if(strcmp("./copy", COMMANDDEFINE) == 0)
{
// copy(source, dest);
}
else if(strcmp("./move", COMMANDDEFINE) == 0)
{
int i = 1;
printf("Test 1: %s\n", argv[i]);
printf("Test 2: %s\n", argv[argc-1]);
move(SOURCEDEFINE, DESTDEFINE);
printf("%s Filename debug \n", SOURCE2DEFINE);
move(SOURCE2DEFINE, DESTDEFINE);
// i++;
}
return 0;
}
void moveMultiple(int argc, char** argv){
int index = 1;
while(argv[index] != NULL){
if(index < argc - 1){
move(argv[index],argv[argc - 1]);
index++;
}
}
}
void move(char *source, char* dest)
{
printf("Running Move\n");
// FILE *s = fopen(source, "r");
// FILE *s;
//FILE *s = fopen(source, "r");
strcat(dest, fileGetName(source));
int l = link(source, dest);
//if(s == NULL)
if(l)
{
printf("Error, File Not Found");
perror("Link");
fflush(stdout);
exit(1);
}
remove(source);
}
void copy(char *source, char* dest)
{
printf("Running Copy\n");
strcat(dest, fileGetName(source));
int l = link(source, dest);
//if(s == NULL)
if(l)
{
printf("Error, File Not Found");
perror("Link");
fflush(stdout);
exit(1);
}
}
char* fileGetName(char *filename)
{
int i = 0;
int length = strlen(filename);
char *catString;
int index = getIndex('/', filename);
index--;
memcpy(catString,&filename[index], length);
return catString;
}
int getIndex(char i, char *s)
{
printf("Running getIndex\n");
int index = -1;
for(int l =0; l<strlen(s); l++){
if(s[l] == i) {
index = l;
}
}
return index;
}
Your move method changes dest (which is really argv[i]), and overwrites the memory after it: strcat(dest, fileGetName(source));. This destroys the other parameter and probably some other things. Don't write strings into memory you don't own.

Read from a file and store it in 2d array

I am working on a function that reads from a file (fp) and stores in the words array. I declared MAX_WORD_SIZE as 128, but when I input any file into this function and check with valgrind, it tells me I have an uninitialized value in the line "while(getline(&line,&count,fp)!=-1)" I really don't get it: what is the uninitialised value? My fp file is valid and the word array is also declared well. Thank you in advance.
#include "functions.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_file(FILE *fp, char words[][MAX_WORD_SIZE + 1], int size) {
int i=0;int j=0;
size_t count=MAX_WORD_SIZE;
char* line=malloc(MAX_WORD_SIZE);
while(getline(&line,&count,fp)!=-1){
for(;count>0;count--,j++){
sscanf(line,"%c",&words[i][j]);
}
i++;
}
int totalNums = i;
int totalNum = j;
if (i<size){
return 1;
}
fclose(fp);
return 0;
}
This is the function that I called this read_file function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
int main(int argc, char *argv[]) {
const char* fileName = argv[1];
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Invalid input file\n");
return 1;
}
int size = 0;
int validity = fscanf(fp, "%d", &size);
int returnValue = 0;
char words[size][MAX_WORD_SIZE + 1];
if(validity != 1 || size <= 0) {
printf("The first line is not a valid number\n");
return 1;
}
returnValue = read_file(fp, words, size);
if (returnValue == 1) {
fclose(fp);
return 1;
}
return 0;
}

How to reformat code to output properly?

I have code that looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
int x;
if (argc != 2) {
printf("error\n");
return -1;
}
char *ptr = argv[1];
int count[256] = {0};
while (*ptr) {
if(!isdigit(*ptr)){
count[(unsigned char)*ptr]++;
ptr++;
}
else{
printf("error\n");
return -1;
}
}
int i;
int compCount = 0;
for (i = 0 ; i != 256 ; i++) {
if (count[i]) {
// compCount += printf("%c%d",i,count[i]);
compCount +=2;
}
}
int j;
if(compCount > strlen(argv[1])){
printf("%s\n", argv[1]);
}else{
for(j=0; j!=256;j++){
if(count[j]){
printf("%c%d",j,count[j]);
}
}
}
}
I am trying to work through some test cases that I was provided. For example, my code breaks at this test case:
INPUT: ./program aabccccccggeeecccccd
EXPECTED: a2b1c6g2e3c5d1
OUTPUT: a2b1c11d1e3g2
Any suggestions as to how I can fix this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// dst needs to at least strlen(src)*2+1 in size.
void encode(char* dst, const char* src) {
while (1) {
char ch = *src;
if (!ch) {
*dst = 0;
return;
}
size_t count = 1;
while (*(++src) == ch)
++count;
*(dst++) = ch;
dst += sprintf(dst, "%zu", count);
}
}
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage\n");
return 1;
}
const char* src = argv[1];
char* dst = malloc(strlen(src)*2+1);
encode(dst, src);
printf("%s\n", dst);
free(dst);
return 0;
}
$ gcc -Wall -Wextra -pedantic -std=c99 -o a a.c
$ ./a aabccccccggeeecccccd
a2b1c6g2e3c5d1
$ ./a abc
a1b1c1
The code does not even compile, because it had 2 errors. The main one was that the esdigit function was defined but the liberia that mentions this procedure, which is ctype (#include <ctype.h>), was not included.
Another error was that a variable x was declared but not used
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "error\n");
return 1;
}
char *ptr = argv[1];
int count[256] = {0};
while (*ptr) {
if(!isdigit(*ptr)){
count[(unsigned char)*ptr]++;
ptr++;
}
else{
printf("error\n");
return -1;
}
}
int i;
size_t compCount = 0;
for (i = 0 ; i != 256 ; i++) {
if (count[i]) {
compCount +=2;
}
}
int j;
if(compCount > strlen(argv[1])){
printf("%s\n", argv[1]);
}else{
for(j=0; j!=256;j++){
if(count[j]){
printf("%c%d",j,count[j]);
}
}
}
printf("\n");
return 0;
}

Conflicting types for a function and also getting too few arguments to function call

I have provided a prototype before main & defined the function after main with the exact same number of arguments. I'm also calling the getline function with the same number of arguments. But I'm still getting conflicting types error & also too few arguments error.
#include <stdio.h>
#define MAXLINE 1000
int getline (char line[], int maxline);
void copy (char from[], char to[]);
int main(int argc, const char * argv[]) {
// insert code here...
int len;
int max;
char currentline[MAXLINE];
char longestline[MAXLINE];
max=0;
while ( (len= getline(currentline, MAXLINE) ) > 0) {
if (len > max) {
max = len;
copy(currentline,longestline);
}
}
if (max > 0)
printf("Longest Line : %s",longestline);
return 0;
}
int getline (char line[],int maxline) {
int c,i;
i=0;
while((c=getchar())!='0' && (c=getchar()) !='\n') {
if (c !='\n' && i < maxline-1) {
line[i]=c;
}
}
if (c == '\n') {
line[i++]=c;
line[i]='\0';
}
return i;
}
void copy (char from[], char to []) {
int i;
i=0;
while((to[i]=from[i])!='\0')
i++;
}

Ragged Arrays and For Loop errors

I get a bad access error in the middle of the for loop, always when i=4. Does anybody know the reason for this? It works until i=4, but I don't see why I wouldn't get the bad access error in any other part of the for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
It looks to me as if this:
m=strlen(*list);
should be:
m=strlen(list[i]);
And this:
*list++;
should not be there at all.

Resources